imagec上文本中的两个级别的y轴刻度标签 [英] Two levels of y-axis tick labels in text on imagesc
问题描述
我正在根据4x5矩阵中的值绘制一个图形,该图形是颜色/阴影的网格.
x轴和y轴刻度标签是使用单元格数组中的文本设置的.
y轴刻度标签存在2个级别,
% y ticks, 2 levels
ylabelnames = {'team1','set1';'team1','set2';'team2','set1';'team2','set2'};
我希望y轴刻度标签为
1)跨2行,因此它们与覆盖在图的正方形中的2行文本匹配,即,每行第一行的"team#"和第二行的"set#" imagesc网格,或
2)旋转标签'team1'跨越前两行,旋转标签'team2'跨越后两行,以免在标签中重复使用'team1''team2'.>
我的整个代码:
%% Plot
Matrix = rand(4,5);
Matrix2 = rand(4,5);
% y ticks, 2 levels
ylabelnames = {'team1','set1';'team1','set2';'team2','set1';'team2','set2'};
xlabelnames = {'group1','group2','group3','group4','group5'};
sigfig = 2;
spacer = '\n';
% Font sizes
plotFontSize = 8; labelFontSize = 8; plotWidth = 500; plotLength = 300;
imagesc(abs(Matrix))
colormap('gray')
colormap(flipud(colormap))
caxis([0 2])
for rows = 1:size(Matrix,1)
for columns = 1:size(Matrix,2)
if abs(Matrix2(rows,columns))<= 0.5 % Show 2nd values less than 0.5
num = Matrix2(rows,columns);
num = vpa(num,sigfig);
num = ['= ' char(num)];
rval = sprintf('%0.2g', Matrix(rows,columns));
message = sprintf([ 'val= ' rval spacer 'val2' num '' ]);
text(columns,rows, message,'HorizontalAlignment','center',...
'FontSize',plotFontSize,'FontName','Times New Roman');
end
end
end
% Put on tick labels
set(gca,'Ticklength', [0 0],'YTick',1:size(Matrix),'XTick',1:length(xlabelnames),...
'YTickLabel',ylabelnames,'XTickLabel',xlabelnames,...
'FontSize',labelFontSize,'FontName','Times New Roman')
如果运行此命令,即使每个imagesc行有足够的空间容纳2行文本,您也会看到仅使用y标签的第一列.
我确实想到了一种可怕的骇客方式,那就是使用
ylabel('team1 team2')
在标签之间留有巨大的空间以将它们均匀地分布在y行之间,但是如果我要增加矩阵的大小,这并不方便,所以我宁愿不那样做.
有什么方法可以实现我需要的多层y刻度标记?
谢谢!
在您的示例中,我将使用 text
函数.例如,您可以添加:
text(-0.05,1.5,'team1','HorizontalAlignment','center','Rotation',90 );
text(-0.05,3.5,'team2','HorizontalAlignment','center','Rotation',90 );
并查看结果.诀窍是要理解每个像素"(或imagesc单元元素)也是函数的文本" x和y输入的单位(如果没有不同的缩放比例,则也是刻度的单位),因此请在第一块和第二块使用y = 1.5或y = 3.5等...
要使用左侧的x值(x = -0.05)等超出图像的左侧,请访问text
解决方案
In your example I'd use the text
function. For example you can just add:
text(-0.05,1.5,'team1','HorizontalAlignment','center','Rotation',90 );
text(-0.05,3.5,'team2','HorizontalAlignment','center','Rotation',90 );
and see the outcome. The trick is to understand that each "pixel" (or imagesc cell element) is also a unit of the function's 'text' x and y inputs (and also of the ticks if you don't have different scaling), so to go between the first and second blocks use y=1.5 or y=3.5 etc...
to go outside the image on the left use a negative x value (x=-0.05) etc. Read more on text
properties here...
这篇关于imagec上文本中的两个级别的y轴刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!