How to fix a colormap
31 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
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?
More Answers (0)
See Also
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!