Reputation: 52229
I use the following code in Matlab to create a figure consisting of 3 subplots. Each subplot is a heatmap. Normally, all the values displayed with the heatmap are in the range from 1 to 6. The problem is that sometimes, data for one of the subplots does not contain all the values. This results in different colorbars for the subplots.
clf
set(gcf, 'Color', 'None');
set(gca, 'Color', 'None');
set(gca, 'LooseInset', get(gca, 'TightInset'));
subplot(3,1,1);
heatmap(values1, [], [], [], 'ColorMap', @cool, 'NaNColor', [0 0 0], 'ColorBar', true);
subplot(3,1,2);
heatmap(values2, [], [], [], 'ColorMap', @cool, 'NaNColor', [0 0 0], 'ColorBar', true);
subplot(3,1,3);
heatmap(values3, [], [], [], 'ColorMap', @cool, 'NaNColor', [0 0 0], 'ColorBar', true);
fname = 'path';
saveas(gca, fullfile(fname, filename), 'png');
How can I use the same colobars for all 3 subplots?
Upvotes: 1
Views: 4610
Reputation: 4558
Except for the fantastic link on the other answer, I would like to give you some two ways to modify the colorbar. The range of the colorbar is set in the axes.
function test()
[xx,yy,zz]=peaks();
h_surf = surf(xx,yy,zz);
h_cbar = colorbar();
h_axes = get(h_surf,'Parent');
pause(2);
set(h_axes,'CLim',[0,8]);
pause(2);
set(h_cbar,'Limits',[4,8]);
Setting the property CLim
like this is the same as using the function caxis
. You can also change the limits on the colorbar to show a special range of the colorbar. These two alternatives should be required and sufficient to customize the range of the colorbar.
I also wish to give you a warning. Do not use the functions gcf
and gca
. These returns the current figure which might not be the one you think is the current. Human interaction (and maybe other things) can change the current figure (eg by clicking on it). This means that even if the code looks great, the output might not be the expected output. This problem seems to have grown worse after 2014b due to some changes in how the graphics is handled. However, the function gca
tends to cause much less problems than gca
since it is naturally much more complicated to interactively change the current axes on a figure, than the current figure itself.
Upvotes: 2
Reputation: 11218
You can define colorbar options in this way: http://www.mathworks.com/help/matlab/ref/colorbar.html?searchHighlight=colorbar%20plot
by the way you can go different ways:
1. if you can change values1,2,3
you can set '1' or any other minimal limit of colorbar scale.
2. use the way like in this link - you can define this limits by your hands but still will get black rectangles for empty values.
Upvotes: 0