Code covered by the BSD License  

Highlights from


4.7 | 11 ratings Rate this file 34 Downloads (last 30 days) File Size: 3.37 KB File ID: #14290
image thumbnail




15 Mar 2007 (Updated )

Real filled colorbar

| Watch this File

File Information

Similar to the well known colorbarf but more realistic.

There are two things missing in colorbar and colorbarf:
1. the colorbar ticks are scaled according to the size of each interval.
2. when color is saturated at the upper limit, or when is white, below the lower limit, a triangle is added. The triangle means "more/less than". Such information is not provided by the existing tools, colorbar and colorbarf.
See the examples.

Besides, there is a bug (?) with contourf: in some cases, even with the levels specified, one additional level is added and corrensponds to the sourrounding line! In such case, colorbarf adds one box for this fake level!


Updated 13-7-2007 by adding the option SCALE for linear/non linear scale


Colorbarf inspired this file.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (20)
26 Feb 2014 Benoit

I had a few issues because I tried to modify the low value triangle to be of the right color instead of white.

I dont know if anybody plans to modify this somewhere, but this is the way i solved the problem :

1) I changed the white triangle color to the corresponding low value color
2) I changed the color of the rectangles to be defined by the value of L(i) + L(i+1) / 2 instead of just L(i), so that the lower triangle does not have the same color as the lowest rectangle.
3) I then change the colormap used in the plot because in contourf the colors used to fill are the colors of the lower limit, (the color of L(i), and not L(i) + L(i+1)), so I take for example the jet map, and chop a part of the top and place it at the bottom.

02 Nov 2013 Kristian

There is a problem with the coloring function, which gives several percent error in the RGB values of the colors in the produced colorbar compared to the colors in the contour. This can be confirmed by running the example script in the cbarf.m header, taking a screenshot of the output and comparing the colors in photoshop or similar.

I have made a workaround by manually setting the Cdata properties of the patch objects returned by cbarf to be equal to the corresponding Cdata properties of the contour patches.

The following script first plots the example included in cbarf.m with the faulty colors, followed by the corrected version using the workaround:

L=[-5:-1 1:5];

clim = [-10 10];


contourPatches = get(h,'Children');
contourCdataMat = cell2mat(get(contourPatches,'Cdata'));
for k=1:length(cbarfh)
if isfinite(sortedContourCdata(k))

29 Oct 2013 Kristian

Awesome script. I was experiencing the annoying contourf bug mentioned in the description, and this script took care of the issue as promised!

13 Oct 2013 Poulomi


I am getting error for my plot, "Conversion to double from struct is not possible." I have 100x100 matrix of data & 16 as level/L.

30 Nov 2011 Christopher Wolfe

By the way, cbarf is otherwise great!

30 Nov 2011 Christopher Wolfe

I have the same problem as Sam. It's due to the use of yt=intersect(yt,L(a:b+1)) on line 214 to select the labels. Somehow rounding errors can make yt and L different by as small multiple of eps, so these labels are excluded by intersect. I work around it by manually resetting the colorbar's 'YTick' property after a call to cbarf.

27 Jul 2011 Sam

When I use cbarf the levels are not labeled on the colorbar. I have levels -0.5:0.1:0.5. and for some reason only 0.2 is labeled. Any suggestions on how to correct this? Other than that, this is a great tool. Exactly what I have been looking for.

02 Jul 2010 Manoj

Oh that's default not bug of cbarf. HSV has circular colour pattern.

02 Jul 2010 Manoj

When I use HSV colormap the lowest and highest value band getting same color.

24 Jun 2010 Makarand

I have one problem, can we use it with pcolor
I tried it to use with pcolor as follows
L=[10 20 50 60 70 100];
The yellow (an extra) color is there on plot but not in the colorbar.
I have one more question: is it possible to equally space the colors in the colorbar

21 Jun 2010 Manoj

Excellent job, but it always get behind the plot on printing or saving. Need to fix it.

29 Nov 2009 Joseph MacGregor

Works as advertised. I used it to produce a discretely sampled (rather than continuous) colorbar for a contourf figure. The resulting colorbar didn't behave ideally after an axes position shift, but I was able to cheat by two copies of the figure (one with a continuous colorbar and the other discrete) and copy/pasting the discrete colorbar onto the better behaved continuous figure. I didn't need the third and fourth inputs. Honestly, it seems like it should be able to work with just the second input (LEVELS) somehow...

Thanks very much for producing this script.

29 Sep 2009 Chamane

great work!

06 May 2009 Brian Magi

A really useful function for a much better colorbar option. This is probably a simple change, but i don't know enough about handle graphics. When i change the position of the colorbar from cbarf to somewhere in the plot, cbarf is always behind the main plot. I tried changing 'Layer' to top (and the main plot to bottom) but to no avail. if anyone has what is probably a quick solution, i'd appreciate it.

22 May 2008 Wu Zhiyong

Great work! Thank you!

14 May 2008 M MA

You are right Wu. The bug is now fixed.
Thank you

14 May 2008 wu only

Thank u for your work!
but I also find a bug(?) with it.

cbarf(data,lev,'vertical','nonlinear') and
cbarf(data,lev,'vertical','linear') produce different colors according to the same specified levels. The latter one is right.

02 May 2008 K Kats

I have been thinking about switching to another tool for contour plots, but this neat script saved me! Thank you.

19 Jul 2007 Lucio Rezende

grande martinho ! excelente !

04 Jun 2007 Loic Jullion

Great!Exactly what I needed!

17 Jul 2007

Added input argument SCALE for linear/non linear scale

Contact us