File Exchange

image thumbnail


version (3.37 KB) by M MA
Real filled colorbar


Updated 14 May 2008

View License

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

Cite As

M MA (2021). cbarf (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (22)


another similar function is colorbar_levels

Tim Garrett

Great function.

Is something missing in that specifying the axis for the color bar is not an option? I'm running into troubles using this function with others on the file exchange with the color bar being placed on an unexpected axis.

As a suggestion:



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.


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))


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



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.

Christopher Wolfe

By the way, cbarf is otherwise great!

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.


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.


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


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


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


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

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.


great work!

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.

Wu Zhiyong

Great work! Thank you!


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

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.

K Kats

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

Lucio Rezende

grande martinho ! excelente !

Loic Jullion

Great!Exactly what I needed!

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: colorbarf

Community Treasure Hunt

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

Start Hunting!