I am plotting 9 subplots as shown in figure below with one color bar for three subplots.
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.
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.
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.
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. This is for ticklabels of axes and would require some tweaking to work forcolorbar
.
or a relatively simpler approach would be to:
Change the
'TicksMode'
to manual i.e.:
Before this lineh.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:
- Set the
PaperUnits
/PaperPosition
at the beginning of the plotting instead of doing that at the end. This will not automate the ticklabels but will only make the temporary adjustment.
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:
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.
来源:https://stackoverflow.com/questions/47951188/error-in-matlab-colorbar-tick-labeling