Matlab: colorbar of contourplot only for specified contour levels
7 次查看(过去 30 天)
显示 更早的评论
I plotted a contour plot in Matlab with data stored in in A, B and C.
contourf(A,B,C,'LineColor','none')
The data stored in C contains values which are 1, 45, 600 and 1000. So the contour plot has only 4 different contour levels. So far as good.
No my issue: I want to show a colorbar with only the 4 contour levels and only the particular colors for 1, 45, 600 and 1000. Therefore I used the code:
colorbar('Ticks',[1,45,600,1000]);
Unfortunatly, Matlab is now plotting a gradient colorbar from 0 to 1000 (gradient colors) and shows only the ticks defined in the code above at the position between 0 and 1000.
How can I get a colorbar showing only the 4 contour levels without a colorgradient?
Thanks in advance!
1 个评论
Dyuman Joshi
2023-10-19
Could you share the data you are working with? Use the paperclip button to attach.
回答(2 个)
Florian Bidaud
2023-10-19
编辑:Florian Bidaud
2023-10-19
f = figure(1);
c = colorbar;
f.Children(2).CLim = [0 1000];
c.Ticks = [2 45 600 1000];
color_array = [0 0 1; 0 1 0; 1 1 0; 1 0 0];
f.Colormap = [repmat(color_array(1,:),c.Ticks(1)+1,1); repmat(color_array(2,:),c.Ticks(2)-c.Ticks(1)-1,1); repmat(color_array(3,:),c.Ticks(3)-c.Ticks(2),1); repmat(color_array(4,:),c.Ticks(4)-c.Ticks(3),1)];
You can choose the colors with the color_array, with 1x3 vectors of values from 0 to 1.
Then you can plot your contour :)
EDIT: if you know you have only these four values, you could even do something like this:
f = figure(2);
c = colorbar;
f.Children(2).CLim = [0 1000];
c.Ticks = [2 45 600 1000];
c.TickDirection = 'none';
color_array = [0 0 1; 0 1 0; 1 1 0; 1 0 0];
f.Colormap = [repmat([0 0 0],max(0,c.Ticks(1)-10),1); repmat(color_array(1,:),18,1); repmat([0 0 0],max(0,c.Ticks(2)-c.Ticks(1)-20),1); repmat(color_array(2,:),18,1); repmat([0 0 0],max(0,c.Ticks(3)-c.Ticks(2)-20),1); repmat(color_array(3,:),18,1); repmat([0 0 0],max(0,c.Ticks(4)-c.Ticks(3)-12),1); repmat(color_array(4,:),18,1)];
2 个评论
Florian Bidaud
2023-10-19
编辑:Florian Bidaud
2023-10-19
Try ploting the contour before everything else like this, and with the right levels.
f = figure(2);
contour([45 45 45; 45 600 1000; 1000 45 2;1000 1000 0; 1000 500 400],[2 45 600 1000])
c = colorbar;
f.Children(2).CLim = [0 1000];
c.Ticks = [2 45 600 1000];
c.TickDirection = 'none';
color_array = [0 0 1; 0 1 0; 1 1 0; 1 0 0];
f.Colormap = [repmat([0 0 0],max(0,c.Ticks(1)-10),1); repmat(color_array(1,:),18,1); repmat([0 0 0],max(0,c.Ticks(2)-c.Ticks(1)-20),1); repmat(color_array(2,:),18,1); repmat([0 0 0],max(0,c.Ticks(3)-c.Ticks(2)-20),1); repmat(color_array(3,:),18,1); repmat([0 0 0],max(0,c.Ticks(4)-c.Ticks(3)-12),1); repmat(color_array(4,:),18,1)];
Star Strider
2023-10-19
I cannot get the ticks exactly where I would like them, even doing my best to interpolate them.
Perhaps this —
[X,Y,Z] = peaks(20); % Create Data
[z1,z2] = bounds(Z(:));
Z = (Z-z1*0.95) * 2E+2;
Lvls = [1 45 600 1000]; % Define Levels
figure
% contour(X, Y, Z, Lvls, 'ShowText',1)
contourf(X, Y, Z, Lvls, 'ShowText',1)
colormap(turbo(numel(Lvls)))
hcb = colorbar;
tx = hcb.Ticks;
txv = interp1([0 1],[min(tx) max(tx)],(0:numel(Lvls)-1)/(numel(Lvls)-1)); % Interpolate Tick Positions
hcb.Ticks = txv;
hcb.TickLabels = Lvls;
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!