How to discretize the color in contour bar at smallest possible range?

25 次查看(过去 30 天)
I am trying to put the different color at smallest ranges like 0.0005-0.005, 0.005-0.009, 0.01-0.05, 0.05-0.1, 0.1-0.25, 0.25-0.5, 0.5-0.75, 0.75-1, 1-1.5, 1.5-2, 2-2.5, 2.5-3. Now, I get the plot in which fill the same color in range between 0-0.5 but I want to decrease the range 0-0.0001 in which only one color fill then it will change for other range like 0.0001-0.005. how to do it?
I write the code in given below. Thank you
contourf(X,Y,Z)
cmap = jet(200);
set(gca, 'Colormap',cmap)
cb = colorbar();
caxis([0,3])
set(cb, 'Ticks', 0:0.25:3)
ylabel(cb, 'Att rate')

回答(2 个)

KSSV
KSSV 2023-2-3
% Make data for demo
[X,Y,Z] = peaks ;
vals = [0.000 0.005;
0.005 0.009 ;
0.01 0.05 ;
0.05 0.1;
0.1 0.25;
0.25 0.5 ;
0.5 0.75 ;
0.75 1 ;
1 1.5 ;
1.5 2 ;
2 2.5 ;
2.5 3] ;
% As Z is in different range get it into range [0 3] here
Z = (3-0)*(Z-min(Z(:)))./(max(Z(:))-min(Z(:)))+0 ;
Z0 = Z ;
% Change the values in Z according to color
for i = 1:size(vals,1)
idx = Z0 >= vals(i,1) & Z0 < vals(i,2) ;
Z(idx) = vals(i,2) ;
end
contourf(X,Y,Z0)
cmap = jet(size(vals,1));
set(gca, 'Colormap',cmap)
cb = colorbar();
set(cb, 'Ticks', 0:0.25:3)
ylabel(cb, 'Att rate')
  1 个评论
DGM
DGM 2023-2-3
Inspection shows that the plot colors don't visually correspond to the intervals shown on the colorbar, as there are 7 levels in the plot, but 12 intervals on the colorbar.

请先登录,再进行评论。


DGM
DGM 2023-2-3
Since you didn't specify the levels or even the number of levels in the call to contourf(), there is no reason to suspect that they occur at particular points on some quasi-log scale. Otherwise, the colorbar would be nothing but misleading. You'll have to specify your levels in the call to contourf(), specify an appropriate-length colormap, and set the colorbar ticks accordingly.
See this example:

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by