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...

 Akzeptierte Antwort

Star Strider
Star Strider am 21 Dez. 2021

0 Stimmen

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 Kommentare

Carles
Carles am 22 Dez. 2021
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).
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 am 22 Dez. 2021
Very appreciated.
Thank you so much for your time on this!
Star Strider
Star Strider am 22 Dez. 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by