How to fix a colormap

31 views (last 30 days)
Stephan
Stephan on 4 Nov 2021
Edited: Cris LaPierre on 8 Nov 2021
Dear all,
the following toy problem
figure; hold on;
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z);
colorbar;
zlim([-1,1]);
generates a colored figure. I would like to keep these colores "fixed".
However, when I add another surf plot as follows
Znew = Z+5;
surf(X,Y,Znew);
the colors change.
Thanks for any help!

Accepted Answer

Cris LaPierre
Cris LaPierre on 4 Nov 2021
Your colormap isn't changing, but by default MATLAB scales the colormap to the range of data in the plot. If your first surf plot, the Z data ranges from -2:2. So when that is the only plot, blue corresponds to -2 and yellow to 2.
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z);
colorbar
However, in your second surface, Znew ranges from 3 to 7. With both surfaces on the plot, blue still corresponds to -2, but now 7 corresponds to yellow. 2 now corresponds to turquoise.
Znew = Z+5;
figure
surf(X,Y,Z);
hold on
surf(X,Y,Znew);
hold off
colorbar
So before we can say how to fix the color scaling, could you explain what it is you want to happen?
  3 Comments
Cris LaPierre
Cris LaPierre on 7 Nov 2021
Edited: Cris LaPierre on 8 Nov 2021
You can use the caxis function to specify the limits of your color range.
Stephan
Stephan on 7 Nov 2021
Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!