开发者

Problem or bug in xticklabel_rotate while drawing heatmap and rotating xtick labels in Matlab

开发者 https://www.devze.com 2023-02-05 20:36 出处:网络
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.

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 hea开发者_高级运维tmap 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):

Problem or bug in xticklabel_rotate while drawing heatmap and rotating xtick labels in Matlab

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.


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消