简体   繁体   English

MATLAB颜色条刻度标签中的错误?

[英]Error in MATLAB colorbar tick labeling?

I am plotting 9 subplots as shown in figure below with one color bar for three subplots. 我正在绘制9个子图,如下图所示,其中一个颜色条表示三个子图。

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}]; 在这里,我想将颜色栏中的最高值显示为> value ,令人惊讶的是,当我手动将刻度标签编辑为h.TickLabels{end} = ['>' h.TickLabels{end}]; the color bar starts repeating the value. 颜色栏开始重复该值。

When I remove h.TickLabels{end} = ['>' h.TickLabels{end}]; 当我删除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. 当我更改set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 8 8])的图形大小时, [0 0 5 5]彩条标签再次更改。

How to resolve this error? 如何解决这个错误?

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

在此处输入图片说明

Why is this happening? 为什么会这样呢?
Manually changing the TickLabels changes the TickLabelsMode property to manual and the control gets lost for zooming/panning/resizing the figure window. 手动更改TickLabels会将TickLabelsMode属性更改为manual,并且控件将因缩放/平移/调整图形窗口而丢失。


How can this be fixed? 如何解决?

  • Use a listener that will adjust the ticks itself. 使用可以自行调整刻度的监听器。 It may require undocumented features. 它可能需要未记录的功能。 You can take ideas on implementing a listener for colorbar from Yair Altman's this utility . 您可以从Yair Altman的this实用程序中获取有关实现colorbar栏监听器的想法。 This is for ticklabels of axes and would require some tweaking to work for colorbar . 这是用于轴的刻度标签,并且需要进行一些调整才能对colorbar起作用。

or a relatively simpler approach would be to: 或更简单的方法是:

  • Change the 'TicksMode' to manual ie: 'TicksMode'更改为手动,即:
    Before this line h.TickLabels{end} = ['>' h.TickLabels{end}]; 在此行之前h.TickLabels{end} = ['>' h.TickLabels{end}]; , include this line: ,包括以下行:

     set(h, 'Ticks', get(h,'Ticks')); %or h.Ticks = h.Ticks; for >= R2014b 

    This ensures that the ticks remain the same and hence the number of ticks also remains the same and therefore ticklabels will not malfunction on zooming/panning/resizing the figure window. 这样可以确保刻度线保持不变,因此刻度线的数量也保持不变,因此刻度线标签不会在缩放/平移/调整图形窗口大小时发生故障。

    If you want to have more or less ticks than you originally get then set them as: 如果您想获得比最初更多或更少的价格变动,请将其设置为:

     %Adjust the increment as desired. I set it as 1 (default) set(h, 'Ticks', in1:1:in2); %or h.Ticks = in1:1:in2; for >= R2014b %where in1 and in2 are the 1st and 2nd input args you used for caxis respectively 

or if you're only concerned with the output jpeg file and your ticklabels are malfunctioned in the output image file then: 或者,如果您只关心输出jpeg文件 ,而输出图像文件中的ticklabel出现故障,则:

  • Set the PaperUnits / PaperPosition at the beginning of the plotting instead of doing that at the end. 在绘图开始时设置PaperUnits / PaperPosition ,而不是在结尾处设置。 This will not automate the ticklabels but will only make the temporary adjustment. 这不会使ticklabel自动化,而只会进行临时调整。

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. 正如Sardar所写 ,更改图形窗口大小时,自动解决此问题且不丢失刻度的自动缩放的唯一选择是添加侦听器。 This is how to do it: 这是怎么做的:

Copy the following function to an m-file, and save it in the folder you work on this figure (ie your current path): 将以下函数复制到m文件,并将其保存在此图上的文件夹(即当前路径)中:

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. 此解决方案比在添加“>”之前先获取刻度更好,因为现在如果窗口变大,则会自动用更多的刻度填充颜色栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM