How to set colorbar limits?

2,448 次查看(过去 30 天)
Anna Shtengel
Anna Shtengel 2015-10-12
回答: Star Strider 2015-10-12
Hi, I'm having trouble using caxis[min max]. What I want to do is manifest the result as follows:
The first color in the colormap will correspond to a min value that I set manually, and the last color to the max value. I also want to draw a colorbar beside the axis I draw the data.
I don't know why the limits are set in a weird way to some previous axis drawn data min and max, regardless the values I specify in caxis:
figure(desiredFig);
(some surface plot);
colorbar;
caxis([min max]);
Thanks for the attention

回答(2 个)

Image Analyst
Image Analyst 2015-10-12
编辑:Image Analyst 2015-10-12
The whole color scale that you specify, whether jet or hot or parula or autumn or whatever, will be applied between the min you pass to caxis and the max you pass to caxis. Values of the array higher than the max will appear as the top color, and values less than the min will appear with the bottom color.
Try this demo and maybe it will help you understand:
hFig = figure(); % Bring up a new figure (unnecessary if none exist yet).
z = peaks(30);
zMin = min(z(:))
zMax = max(z(:))
subplot(1,2,1);
surf(z);
colormap(jet(256));
colorbar;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
subplot(1,2,2);
imshow(flipud(z), []);
axis on;
set(gca, 'ydir', 'reverse');
colormap(jet(256));
colorbar;
caxis([zMin, zMax]);
uiwait(msgbox('Now we will make the whole color scale go from -7 to 0 so it will show only negative values in the image on the right'));
subplot(1,2,1);
caxis([-7, 0]);
subplot(1,2,2);
caxis([-7, 0]);

Star Strider
Star Strider 2015-10-12
I’m not certain what problem you are having.
I haven’t used the cmap function much, but it refers to mapping your data to the colormap, not the colormap itself. If your data are ‘x’, and you want the colormap so that all values below 0.25*x are mapped to the ‘low’ colour, and all values above 0.75*x are mapped to the ‘high’ colour, the cmap call is: cmap(min(x)+[0.25 0.75]*[max(x)-min(x)]).
If you want to define the colormap to have fewer colours that the default (or a certain specific set of colours), you just have to tell it. See the documentation for colormap for details. The colormap itself is a (Nx3) matrix.
(Please do not use min and max as variable names. They are built-in MATLAB functions, and will cause problems if you want to use the functions later in your code.)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by