フィルターのクリア

Legend with 15 colors

17 ビュー (過去 30 日間)
Ash Ahamed
Ash Ahamed 2021 年 11 月 10 日
コメント済み: Star Strider 2021 年 11 月 10 日
Hello,
How can I make a 3D plot legend to have 15 colors/levels and seperated/discreet like the attached example?
Thanks in advance!

採用された回答

Star Strider
Star Strider 2021 年 11 月 10 日
Try this —
x = linspace(0, sqrt(300), 15);
y = x;
z = x(:)*y;
figure
surf(1:15, 1:15, z)
grid on
colormap(turbo(15))
colorbar
I am not certain what colormap was used in the posted question. I chose turbo here.
Experiment with the actual data.
.
  2 件のコメント
Ash Ahamed
Ash Ahamed 2021 年 11 月 10 日
Thank you!
Star Strider
Star Strider 2021 年 11 月 10 日
As always, my pleasure!
.

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

その他の回答 (1 件)

DGM
DGM 2021 年 11 月 10 日
編集済み: DGM 2021 年 11 月 10 日
I don't know what your plot type is, but consider the contourf example:
% the given colormap
cmap = [0.851 0.851 0.851;0.498 1 1;0.298 0.8 1;0 0.6196 1;0 0 0.9529;0.498 1 0.498;0.09804 1 0.09804;0.09804 0.6 0.09804;1 0.749 1;1 0.4 1;0.8 0.09804 1;1 1 0;1 0.651 0;0.9529 0.298 0;0.8 0 0];
% dummy data
x = linspace(0,1,100);
y = x.';
z = max(x,y);
% plot setup
contourf(x,y,z,size(cmap,1)-1)
axis equal
colormap(cmap)
% wrangle the colorbar tick alignment
cb = colorbar;
cbscalein = cb.Limits;
cbscaleout = [min(z(:)) max(z(:))];
ticks = linspace(cbscaleout(1),cbscaleout(2),size(cmap,1)+1);
cb.Ticks = diff(cbscalein)*(ticks-cbscaleout(1))/diff(cbscaleout) + cbscalein(1);
cb.TickLabels = diff(cbscaleout)*(cb.Ticks-cbscalein(1))/diff(cbscalein) + cbscaleout(1);
That may be able to be simplified, but I'll leave that to someone else to reveal.
  1 件のコメント
Ash Ahamed
Ash Ahamed 2021 年 11 月 10 日
Thank you! your solution works perfect as well.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by