How to fix a colormap

19 ビュー (過去 30 日間)
Stephan
Stephan 2021 年 11 月 4 日
編集済み: Cris LaPierre 2021 年 11 月 8 日
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!

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 11 月 4 日
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 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 11 月 7 日
編集済み: Cris LaPierre 2021 年 11 月 8 日
You can use the caxis function to specify the limits of your color range.
Stephan
Stephan 2021 年 11 月 7 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by