File Exchange

image thumbnail


version (4.39 KB) by Blair Greenan
Add an accurate colorbar to your filled contour plot.


Updated 04 Apr 2006

View License

Editor's Note: This file was a File Exchange Pick of the Week

This m-file produces a colorbar with solid blocks of color between the contour levels that correspond precisely to the colors in the filled contour plot. This is in contrast to the standard MATLAB colorbar which simply plots all the colors in the colormap and, therefore, only approximates what is being plotted in the filled contour plot.

Cite As

Blair Greenan (2021). colorbarf (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (18)

T. U.

Mahmut Ruzi

great code, simple and does what it claims to do. Thanks



Albert Drif

if there is any NaN in the variable, the colorbar shows white block. I didn't find a way to remove the block except Photoshop.

Albert Drif

Albert Drif

does it support log scale? Like colorbar('Yscale','log')? I know I can convert the data to log10 first, but the ticks of colorbarf becomes -2 -1 0 1 2, which I want it display as 10^{-2} 10^{-1} 10^{0} 10^{1} 10^{2}, like in Latex. Thx

Shannon Fargey

can colourbarf be used with pcolor instead of contourf? if so what modifications would have to be made?

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.

onur kerimoglu

Exactly what I was looking for. I don't understand why it was not built-in. Thanks.

Alan Drew

Every time I plot a slightly different subset of my array (e.g from contourf(y(50:150),x(50:150),positron(50:150,50:150)) to contourf(y(70:130),x(70:130),positron(70:130,70:130)) ) the axis get smaller but the colorbarf remains the same size. As a consequence, if I am optimising the display of my figure, I have to keep closing the figure window.

Jean-Marie Ndambi

Dan S

Can this utility be modified to work with the surf function?

I need a color bar on a surface plot but the scale on the built in colorbar is inaccurate since it's divided into 64 segments instead of the 5 integer divisions that I need.

r pearson

This program was not working when I downloaded it. I would get the error:
Error in ==> colorbarf at 151
colors(j) = get(H(j),'CData'); % get the color used to fill the
This was with version 7 even if I used the 'v6' option.
I made these changes and it works now:
%Modded by R Pearson
for j = 1:length(H)
% colors(j)= get(H(j),'CData'); % get the color used to fill the patches
tempcolors = get(H(j),'CData'); % get the color used to fill the patches
%%%%%End Pearson

I worked after this but I don't know if it is the right way to do this. I don't endorse this method at all. It simply works for me in my limited usage.

Felix Jose

Very handy and expressive for presentations

Robert Weinmann

Very good.

Nikolaos Skaropoulos

Mads Hvid Nielsen

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

Inspired: cbarf

Community Treasure Hunt

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

Start Hunting!