Dear MATLAB users,
I would like to plot 3D data as a contour plot but struggle with colorbar/colormap settings.
Using R2017a, following code creates a filled 2D contour plot but for z =0 it assigns same color as for z=1.
[X,Y] = meshgrid(0:0.5:10);
Z = X + Y;
z_int=fix(Z/2);
max_z_int=max(max(z_int));
min_z_int=min(min(z_int));
contourf(X,Y,z_int,max_z_int-min_z_int);
colormap(parula(max_z_int-min_z_int))
c=colorbar;
caxis([min_z_int max_z_int])
If I add an extra color:
colormap(parula(max_z_int-min_z_int+1))
I get different colors on z=0 and z=1 but colorbar ticks are off
so I changed
caxis([min_z_int-1 max_z_int])
but I get again same color on level z=0 and z=1...

 採用された回答

Star Strider
Star Strider 2021 年 12 月 21 日

0 投票

Tell contourf to use more colours —
Levels = hcf.LevelList
colormap(parula(numel(Levels))
and the individual patches become distinct —
[X,Y] = meshgrid(0:0.5:10);
Z = X + Y;
z_int=fix(Z/2);
max_z_int=max(max(z_int));
min_z_int=min(min(z_int));
[~,hcf] = contourf(X,Y,z_int,max_z_int-min_z_int, 'ShowText','on');
Levels = hcf.LevelList
Levels = 1×11
0 0.9091 1.8182 2.7273 3.6364 4.5455 5.4545 6.3636 7.2727 8.1818 9.0909
colormap(parula(max_z_int-min_z_int))
c=colorbar;
caxis([min_z_int max_z_int])
colormap(parula(numel(Levels)))
.

4 件のコメント

Carles
Carles 2021 年 12 月 22 日
Thanks Star Strider for your interest in my question.
With your code is getting better but in fact I realise that as I want the plot to show diferent number of layers, my colorbar should look like below (number of layers and tick in the middle of the color).
Star Strider
Star Strider 2021 年 12 月 22 日
My pleaseure.
That is a different problem not mentioned in the original Question (that I fuly solved).
Try this —
[X,Y] = meshgrid(0:0.5:10);
Z = X + Y;
z_int=fix(Z/2);
max_z_int=max(max(z_int));
min_z_int=min(min(z_int));
[~,hcf] = contourf(X,Y,z_int,max_z_int-min_z_int, 'ShowText','on');
Levels = hcf.LevelList;
colormap(parula(max_z_int-min_z_int))
c=colorbar;
c.Ticks = linspace(0, 10, 12)+0.5; % Tick Locations
TickLabelVector = linspace(0, 10, 11); % Numeric Labels
c.TickLabels = compose('\\it\\bf%g\\rm',TickLabelVector); % Create Tick Label Cell Array
C.FontName = 'Engravers MT'; % Choose Font (Not All Are AVailable Here)
caxis([min_z_int max_z_int])
colormap(parula(numel(Levels)))
The online Run feature doesn’t have all the fonts that might be desired, however this demonstrates everything else with respect to the colorbar tick locations and labels, that are as requested.
.
Carles
Carles 2021 年 12 月 22 日
Very appreciated.
Thank you so much for your time on this!
Star Strider
Star Strider 2021 年 12 月 22 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeColor and Styling についてさらに検索

製品

リリース

R2017a

質問済み:

2021 年 12 月 21 日

コメント済み:

2021 年 12 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by