在 Matlab 中绘制热图和旋转 xtick 标签时 xticklabel_rotate 出现问题或错误

发布于 2024-10-14 02:32:45 字数 2069 浏览 2 评论 0原文

我一直在Matlab中绘制带有标签的热图,主要使用函数imagesc来绘制热图和xticklabel_rotate来旋转xtick标签。 (请参阅此处了解 xticklabel_rotate)。 通常效果很好。但今天我遇到了一些问题,似乎是由 xticklabel_rotate 引起的(或者可能是 xticklabel_rotate 使用的 Matlab text 句柄?)

为了说明这个问题,下面我打印我的代码和从代码(基本上,它的作用是随机生成一个正态分布的数据矩阵,使用imagesc为数据绘制热图,在轴的顶部和右侧绘制其标签,然后旋转xticklabels):

function debug_xticklabelRotate(numX, numY, axisFontsize)
    data = randn(numY, numX);
    imagesc(data);
    colormap(jet);
    box on
    set(gca, 'ticklength', [0,0]);
    set(gca, 'xminortick', 'off', 'yminortick', 'off');
    set(gca, 'XAxisLocation', 'top');
    set(gca, 'YAxisLocation', 'right');
    set(gca,'FontSize', axisFontsize);
    axis image
    set(gca, 'Xtick', 1 : numX);
    htext = xticklabel_rotate([],90, [], 'fontsize', axisFontsize);
    set(gca, 'YTick', 1 : numY);
end

问题1:我用参数调用了上面的函数,输出

debug_xticklabelRotate(40, 100, .5);       

图像如下所示(为了节省空间,我剪切图像并只显示前几行):

请注意,错误在于,由于调用 xticklabel_rotate,图形框的右侧和 yticklabel 均未绘制。

问题2:当我使用参数调用上述函数时,输出

debug_xticklabelRotate(40, 200, .5);  % only numY is changed from 100 to 200

图像如下所示(再次为了节省空间,我剪切图像并只显示前几行):

http://i55.tinypic.com/317grdd.png

与 numY= 的调用相比参见图100,该图中显示了数据图像和标签。但是,请注意这里的两个问题。首先,xticklabel的fontsize与yticklabel的(axisFontsize)不一样。其次,xtick 标签与列 (xticks) 未对齐:有些标签较近,有些标签较远。

请注意,如果我在函数 debug_xticklabelRotate 中删除 xticklable_rotate 的函数调用,所有这些提到的问题都消失了(除了现在 xticklabel 不旋转)。

我希望我的问题在上面说清楚。任何有关解决问题的建议都将受到高度赞赏。非常感谢。

I have been drawing heatmaps with labels in Matlab, mainly using the functions imagesc to draw the heatmap and xticklabel_rotate to rotate the xtick labels.
(please see here for xticklabel_rotate).
It usually works well. But today I met some problem which appeared to be caused by xticklabel_rotate (or maybe the Matlab text handle used by xticklabel_rotate?)

To illustrate the problem, in the following I print my code and the results generated from the code (basically, what it does is to randomly generate a normally distributed data matrix, draw a heatmap for the data using imagesc, draw its labels on top and right of the axis, and then rotate xticklabels):

function debug_xticklabelRotate(numX, numY, axisFontsize)
    data = randn(numY, numX);
    imagesc(data);
    colormap(jet);
    box on
    set(gca, 'ticklength', [0,0]);
    set(gca, 'xminortick', 'off', 'yminortick', 'off');
    set(gca, 'XAxisLocation', 'top');
    set(gca, 'YAxisLocation', 'right');
    set(gca,'FontSize', axisFontsize);
    axis image
    set(gca, 'Xtick', 1 : numX);
    htext = xticklabel_rotate([],90, [], 'fontsize', axisFontsize);
    set(gca, 'YTick', 1 : numY);
end

Problem 1: I called the above function with parameters as

debug_xticklabelRotate(40, 100, .5);       

the output image is shown below (to save space here, I cut the image and only show the top few rows):

Please notice that the bug is that, as the result of calling xticklabel_rotate, neither the right side of the figure box nor the yticklabels are drawn.

Problem 2: When I call the above function with parameters as

debug_xticklabelRotate(40, 200, .5);  % only numY is changed from 100 to 200

the output image is shown below (again to save space here, I cut the image and only show the top few rows):

http://i55.tinypic.com/317grdd.png

Compared to the call with numY=100, in this figure both the data image and the labels are shown. However, please notice two problems here. First, the fontsize of xticklabel is not the same as that of yticklabel (axisFontsize). Second, the xtick labels are not aligned well with the columns (xticks): some labels are closer and some are further away.

Please note if I remove the function call of xticklable_rotate in my function debug_xticklabelRotate, all these mentioned problems are gone (except that now the xticklabel are not rotated).

I wish my problem is stated clear above. Any suggestion on solving the problem will be highly appreciated. Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2024-10-21 02:32:45

作为 xticklabel_rotate 的作者,我尝试重现所提到的错误,但收效甚微。我发现当 y 标签位于右侧时轴位置发生更改的问题,我将对此进行研究。当我运行相同的示例时,我没有得到像您所显示的那样明显的错位,所以我不知道如何回应。请在打印图形时确认这些问题是否存在,并且不是简单的显示问题。

文本框以数据单位在刻度位置创建。它们是中间对齐的,没有什么可以做的,但我愿意接受建议。

附言。我同意如果这个函数是 MatLab 固有的就好了。如果是的话,我就不必做这件事了。

As the author of xticklabel_rotate, I have tried to reproduce the errors mentioned with only small success. I have found an issue with the axis position being changed when y-labels are on the right, which I will look into. I do not get such marked misalignements as you have shown when I run the same examples, so I am not sure how to respond. Please verify that these issues exist when you PRINT the figure, and it is not a simple display issue.

The text boxes are created at the tick positions, in data units. They are middle aligned, there is not much more that can be done, but I am open to suggestions.

PS. I agree it would be nice if this function was inherent in MatLab. If it was, I wouldn't have had to work on this.

任性一次 2024-10-21 02:32:45

如果您注释掉该行:

set(gca, 'YAxisLocation', 'right')

那么它应该按预期工作。看来 XTICKLABEL_ROTATE 函数不支持正确的 y 标签。您应该联系原作者并让他知道可能的错误......

If you comment out the line:

set(gca, 'YAxisLocation', 'right')

then it should work as expected. It seems that the XTICKLABEL_ROTATE function does not support right y-labels. You should contact the original author and let him know of the possible bug...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文