MATLAB颜色条刻度标签中的错误? [英] Error in MATLAB colorbar tick labeling?
问题描述
我正在绘制9个子图,如下图所示,其中一个色条表示三个子图.
I am plotting 9 subplots as shown in figure below with one color bar for three subplots.
在这里,我想在色条中将最大值显示为> value
,令人惊讶的是,当我手动将刻度标签编辑为h.TickLabels{end} = ['>' h.TickLabels{end}];
时,色条开始重复该值.
Here I want to show the highest value in color bar as > value
, surprisingly when I manually edit the tick label as h.TickLabels{end} = ['>' h.TickLabels{end}];
the color bar starts repeating the value.
当我删除h.TickLabels{end} = ['>' h.TickLabels{end}];
时,颜色栏显示没有问题.当我更改set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 8 8])
中的图形大小时,随着[0 0 5 5]
彩条标签再次更改.
When I remove h.TickLabels{end} = ['>' h.TickLabels{end}];
the color bar show no problem. When I change the figure size in set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 8 8])
as [0 0 5 5]
colorbar labeling again changes.
如何解决此错误?
下面是我的工作示例和输出图像:
Below are my working example and output image:
data = [1 2 3; 5 7 3; 12 29 14; 1 7 3; 2 8 3; 5 4 1; 2 2 1; 2 3 1; 1 5 2];
for i=1:9
subplot(3, 3, i)
plot(data(i,:));
if ismember(i, [1:3])
caxis([0 20])
if i==3
h = colorbar('Fontsize', 6, 'LineWidth', 0.15, 'TickDirection', 'out',...
'TickLength', 0.02);
set(h, 'Position', [.935 .6867 .01 .2533])
h.TickLabels{end} = ['>' h.TickLabels{end}];
end
end
if ismember(i, [4:6])
caxis([0 6])
if i==6
h = colorbar('Fontsize', 6, 'LineWidth', 0.15, 'TickDirection', 'out',...
'TickLength', 0.02);
set(h, 'Position', [.935 .3733 .01 .2533])
h.TickLabels{end} = ['>' h.TickLabels{end}];
end
end
if ismember(i, [7:9])
caxis([0 4])
if i==9
h = colorbar('Fontsize',6, 'LineWidth', 0.15, 'TickDirection', 'out',...
'TickLength', 0.02);
set(h, 'Position', [.936 .06 .01 .2533])
h.TickLabels{end} = ['>' h.TickLabels{end}];
end
end
end
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 8 8])
print('test', '-djpeg', '-r300')
close all
推荐答案
如 Sardar所写,这是解决问题的唯一选择这会自动执行,并且在更改图形窗口大小时不会丢失刻度的自动缩放,而是添加侦听器.这是这样做的方法:
As Sardar wrote, the only option to solve this automatically, and not lose the auto-scaling of the ticks when the figure window size is changed is to add a listener. This is how to do it:
将以下函数复制到m文件,并将其保存在此图所用的文件夹中(即当前路径):
Copy the following function to an m-file, and save it in the folder you work on this figure (i.e. your current path):
function set_cb_lables
% add '>' to the last tick label in all colorbars
h = findobj(gcf,'Type','Colorbar'); % get all colorbars handels
set(h,{'TickLabelsMode'},{'auto'}); % change thier mode to 'auto'
tiklbl = get(h,{'TickLabels'}); % get all tick labels after the change
for k = 1:numel(tiklbl)
tiklbl{k}{end} = ['>' tiklbl{k}{end}]; % add '>' to the last tick
end
set(h,{'TickLabels'},tiklbl); % replace the current ticklabels with tiklbl
end
然后,在您的代码中,在循环之后添加此行:
Then, in your code, add this line after the loop:
set(gcf,'SizeChangedFcn','set_cb_lables'); % aplly the function 'set_cb_lables' upon any size change
在调整图形大小后,这将自动在最后一个刻度标签上添加>".
This will add '>' automatically to the last tick label upon any resizing of the figure.
此解决方案比在添加'>'之前先获取刻度更好,因为现在如果窗口变大,则会自动在色条中填充更多刻度.
This solution is better than just getting the ticks before adding the '>' because now if the window gets bigger, the colorbar is populated automatically with more ticks.
这篇关于MATLAB颜色条刻度标签中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!