Clear Filters
Clear Filters

Matlab: colorbar of contourplot only for specified contour levels

6 views (last 30 days)
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!

Answers (2)

Florian Bidaud
Florian Bidaud on 19 Oct 2023
Edited: Florian Bidaud on 19 Oct 2023
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 Comments
Contour
Contour on 19 Oct 2023
Hi Florian,
Thank you for the fast reply! When I plot the contour plot the colorbar colors are not matching the color of the contour levels. :/
In addition, is there a way so that the colorbar is separated in cells of the same size? So the cell between 0 and 1 should have the same size as between 600 and 800 and so on?
Florian Bidaud
Florian Bidaud on 19 Oct 2023
Edited: Florian Bidaud on 19 Oct 2023
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)];

Sign in to comment.


Star Strider
Star Strider on 19 Oct 2023
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;
.

Community Treasure Hunt

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

Start Hunting!