0

I want to change the default color bar (jet color) generated by Matlab, especially the step of the color (just like the figure below). How to do that?

Here's my code

[hC hC] = contourf(interp2(sal,2,'spline'),[0:0.5:5]);
set(hC,'LineStyle','none','YTick',0:4);
colorbar;

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Eko Susilo
  • 250
  • 2
  • 15

2 Answers2

1

If you are looking to reduce the number of colors in the contour plot and colorbar then you can set a new colormap with a reduce color set.

%Get 10 colors from jet
numColors = 10;
colormap(jet(numColors))  
data = peaks;
contourf(data)
% Optionally you can set yTicks in conjunction with the number of items in your colormap to line up
colorbar('YTick',linspace(min(data(:)),max(data(:)),numColors+1))

enter image description here

EDIT: If you want more control over where the contour lines are drawn then use the function in this form countourf(data,v) where v is an monotonically increasing vector of your desired contour levels. Example:

contourf(data,linspace(-7,8,numColors))
c = colorbar('YTick',linspace(-7,8,numColors+1));

The will draw 10 contour lines at -7, -5.33, -3.66 ... 8. Replace -7 and 8 with whatever you wish ex. min/max of data or whatever makes sense for your application

Aero Engy
  • 3,588
  • 1
  • 16
  • 27
  • It's good answer, working perfectly. But with your script `colorbar('YTick',linspace(min(data(:)),max(data(:)),numColors+1))`, its will show the min and max data. How to define specific step for example (6,4,2,0,-2,-4,-6)..@Aero Engy – Eko Susilo Jul 14 '15 at 03:39
  • See my EDIT above. Basically use contourf(data,v) where v is [6,4,2,0,-2,-4,-6] or what makes sense for your data set. I would probably match the numColors to however many contour lines you wish to draw. – Aero Engy Jul 14 '15 at 12:07
0

You can adjust the color bar properties by using:

c=colorbar;
c.Ticks=[vector of tick locations]

or alternately you could try

c.Limits=[min max]

See the MATLAB documentation for colorbar properties: http://www.mathworks.com/help/matlab/ref/colorbar-properties.html?refresh=true

this explains color bar customization in more detail

bern
  • 366
  • 1
  • 10