How to fix a colormap

29 visualizzazioni (ultimi 30 giorni)
Stephan
Stephan il 4 Nov 2021
Modificato: Cris LaPierre il 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!

Risposta accettata

Cris LaPierre
Cris LaPierre il 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 Commenti
Cris LaPierre
Cris LaPierre il 7 Nov 2021
Modificato: Cris LaPierre il 8 Nov 2021
You can use the caxis function to specify the limits of your color range.
Stephan
Stephan il 7 Nov 2021
Thank you!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by