File Exchange

image thumbnail

COLORMAP and COLORBAR utilities (Jul 2014)

version (67 KB) by Carlos Adrian Vargas Aguilera
MATLAB color utilities including COLORMAP join and interpolation; freeze and fit COLORBAR, etc.


Updated 03 Jul 2014

View Version History

View License

These are some utilities that improve the COLORMAP and COLORBAR functions. They are as follows, and each one includes help and examples:

COLORMAP utilites:
cmapping - Colormap linear mapping/interpolation.
cmfit - Sets the COLORMAP and CAXIS to specific color bands.
cmjoin - Joins colormaps at certain levels.
cmlines - Change the color of plotted lines using the colormap.

COLORBAR utilities:
cbfit - Changes COLORMAP and CAXIS to fit between colorbar's ticks.
cbfreeze - Freezes the colormap of a colorbar.
cbhandle - Gets the handle of current colorbar or its peer axes.
cblabel - Adds a label to the colorbar.
cbunits - Adds units (and ISU prefixes) to the colorbar ticklabels.
Following functions I submitted here previously were renamed to:
Enjoy it!
Bugs reports and suggestions will be very appreciated!

Cite As

Carlos Adrian Vargas Aguilera (2021). COLORMAP and COLORBAR utilities (Jul 2014) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (75)

smith lili

Thanks the author, and thanks to Lívia Mendonça for reminding: Yes, for new matlab versions the multiple colormap is native, just use colormap(gca, hot). As far as my version, 2016b, it works well.


Pauline Combarnous

I am using r2016b and have the same problem as Maximilien :
"Error using image
While setting property 'XData' of class 'Image' :
This is not a valid ImageXYData value. The input value can't be Inf
Error in cbfreeze (line 354)
image(X,Y,ind2rgb(CData,cmap),... "
Could you (or anyone) help me ? I really need this !

Maximilien Chaumon

Thanks for the awesome tool.
I have an issue in R2019a in cbfreeze.
It seems like the colorbar object does not have any image descendant, himage is an empty placeholder at line 290, which leads to empty CData at line 293 and crash at line 354 with message:
Error using image
Inf is not supported.
Error in cbfreeze (line 354)

Can you help?

Narendra Reddy Nelli

Narendra Reddy Nelli

David Franco

wei ma

It's awesome!!! It works well!

Zhang Qinghao

Zhang Qinghao

From 2014b versions,matlab can realize it without this function.
For example:
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);

Matthias Pospiech

This code is not compatible with new versions. Even if matlab has support for mutliple colormaps I would however appreciate it to use the plugin since I have too many code which uses it.

Errors such as
XLim = cbprops.XLim;

can be fixed
XLim = get(cbh, 'Xlim');

but for this I have not idea: (result is empty)
himage = findobj(cbh,'Type','image');

ying han

Lívia Mendonça

For new matlab versions the multiple colormap is native, just use colormap(gca, hot), e.g. ;)


Same as others, error when trying to use cbfreeze on M2016b:

K>> cbfreeze
Reference to non-existent field 'XLim'.

Error in cbfreeze (line 332)
XLim = cbprops.XLim;


After some debugging, I've realized my path was not pointing to the latest version of cbfreeze. The latest version is clean for Matlab 2013b. I've changed my previous rating


I'm still on Matlab 2013b and I'm experiencing issues (as it sounds many here are). It's freezing the colormap, but removes all other labels on the colorbar. Also, I can no longer add a new label via "ylabel(cbHand,...". I give it 2 stars for lack of version support. (It was a 5 star when I downloaded it years ago)


It's awesome!!! It works well!

Johannes Rebling

I also vote for 2016 support!

great code indeed!

freezeColors(h1); cbfreeze(cb1)

... allows me to fix the colormaps of a series of subplot images. However, after cbfreeze the colormap handle ('cb1') seems to be lost; (Error message "Invalid or deleted object").
Anyone experienced this too?

thank you

(i am using Matlab 2012b).

Diego Rivera

Great code¡
However, when I updated to R2015b I got the following message for cbfreeze:

XLim does not exist.
cbfreeze (line 332)
XLim = cbprops.XLim;

Lingxiao Zhu

I'm using 2016a(Language Chinese).

Reports XLim does not exist.

cbfreeze (line 332)
XLim = cbprops.XLim;

It happened while using cbfreeze.


Hey, this cool tool could use an update to Matlab 2016a



Liu lb



Tanvi Gupta

Used cbfit to change colorbar levels as per user defined vecor .It worked pretty good for me. Thanks for the useful submission.

Tanvi Gupta

Used cbfit to change colorbar levels as per user defined function .It worked pretty good for me. Thanks for the useful submission.

Jakub Bialek

I have the same problem: two color bars on the same, superimposed figure, with two different colormaps. I managed to freeze the colormap with, but the colorbar assumes only one colormap....


Hello, I am trying to use cbfreeze to place TWO colorbars with different colormaps for the *same plot* (no subplots). I have a red scale colorbar on the westoutside and a greyscale on the eastoutside colorbar location.

Cbfreeze will not allow the colormap for the first colorbar to stay red when I add the grey colorbar to the east. Can anyone help me make this happen?



corrected version:

plot command
plot command


Correction: sorry Tristan, I just realized that from 2014b onwards, subplots are indeed axes in themselves. So you were right.
For everyone else with the same problem, what you need to do is something like:

plot command
plot command

This will give you 2 side-by-side plots with differing colormap and colorbars. Great.


I am not sure if this is true. I think colormaps are still associated to axes not subplots.

@the others: I have the same problem as everyone else. cbfreeze no longer works. I want two different colormaps in a single figure with 15 subplots.
I created 2 axes, but I also use M_map and when I try to change the colormap of an axes, it tells me that the axes handle has been deleted (probably caused by m_map). Does anyone have a similar problem and found a workaround?
For now, the only solution seems to be to create to JPEGs and join them in Photoshop which is not particularly elegant. Cheers.


from Matlab 2014b, colorbars are different objects, no axes. on the upside, you can have different colorbars and colormaps for subplots supported natively now. for maps, use: colormap(ax,map), for colorbars: colorbar('peer',ax).

Manolis Oikonomakis

Same problem here. cbfreeze works fine until version 2014a but in 2014b it doesn't. same error message as Karl posted.


cbfreeze does not seem to work with the 2014b.
Reference to non-existent field 'XLim'.

Error in cbfreeze (line 310)
XLim = cb_prop.XLim;


Using with colorFreeze, this work well. However using it along with a 3d plot (surf)causes the colorbar to have a LOT of numbers all around the figure... This is a shame though since my only use is for such usage...


These functions brings out the real potential of matlab graphics. Kudos.

Thank you all for your comments. Now the scientific notation 'x10' issues are fixed (well I guess so), and a new way to write it included: using International System Units' prefixes (mega, kilo, etc.).

Cheers, Carlos A.



The function works very nicely, especially in combination with the freezeColors function :-)

However, it doesn't work with the lcolorbar. I'm mainly interested in this because I use colors to encode/visualize categories rather than actual values, where lcolorbar is very easy to add a label. Any quick fix in mind? I'll try to fix it by adding labels on the normal colorbar manually in the meantime.




Hi again,
I noticed Carlos post from 27 Feb later. I think he meant line 366 where you add the 'YTickLabel:
cb_prop = rmfield(cb_prop,{...


Has anybody been able to address the order of magnitude question from Jenny (20 Nov 2013)? I'm having the same issue where I have a colorbar with a '10^-6' on the top right which disappears upon the cbfreeze command. I don't know how to fix this.

Felipe Orellana

Hello Carlos Adrian Vargas Aguilera,

My name is Felipe Orellana, a graduate student here at Univ. California at Berkeley. I am doing a PhD in Geophysics, more specifically, I do numerical simulations in plate tectonics.

Man, thanks a lot !!!

your routine is awesome ! a piece of art in matlab programing.

thanks man, you are very helpful,

best wishes,



had a problem with function, this was losing value 'x10' that stands above the colorbar when the values ​​are small, the way it was corrected by adding in cbfreeze.m on line 336 the words 'YTickLabel', 'Ytick' and I served ready. I hope to help someone else.

Happy day: D

Eowyn Baughman

I may have found a workaround for the issue of cbfreeze removing the colorbar label. Calling cbunits before AND after cbfreeze fixed it for me.


I think I have found a way to modify cbfreeze.m to fix the problem of colorbars changing size and losing their label. Does anybody know how to upload a new version? I'll try emailing Carlos directly aswell.

ZZL Zhang


I am using cbfreeze in conjunction with freezeColors on a multi-subplot figure to be able to use different colorbars in each subplot. My problem is that cbfreeze freezes the colorbar nicely, but it removes the order of magnitude for the ticks. Usually the order of magnitude appears as a small number on the top right of the colorbar (say, the ticks go from 10^5-10*10^5, then the tick marks are 1-10 and the 10^5 appears once on the top right of the colorbar for a vertical colorbar). Any suggestions on how I can get my order of magnitude back?
Thanks a lot!

Jim K

Jaya Khanna

Thanks for the useful submission.
Using cbfreeze deletes the colorbar label when used without the function 'cblabel'. Try this: after making a sublot cbfreeze the colorbar then add the label using cblabel. It works for me. Here is an example:

a = rand(10,10);
x = 0:0.1:2*pi; y = 0:0.1:2*pi;
[X,Y] = meshgrid(x,y);
b = cos(X)+sin(Y);

c1 = colorbar;
cblabel('colorbar 1','fontsize',14);

c2 = colorbar;
cblabel('colorbar 2','fontsize',14);

Martina Callaghan

Thanks! Super useful.


cbfreeze deletes my colorbar labels and widens the whole colorbar, and ideas?

Richard Sims

Does anyone have a quick and preferably easy solution to stop my colorbar labels dissapearing after cbfreeze is used, the suggestions below are not easy to follow.

Jakob Sievers

Incredibly useful!

Julien Emile-Geay

It's a clever workaround for one of the most shameful bugs in all of Matlab. Mathworks engineer behold: if your software were written properly, this should never be a problem in the first place. Oh, and every graphic renderer should respect transparency (alpha), that would also be a nice change.

Back to cbfreeze: it works fine but insists on redrawing my horizontal colorbar with a vertical one. Has anyone figured out a fix for that?


Nice work.
the cbfreeze('off') and cbfreeze('del') seem to apply to the whole figure instead of only the current axes. cbfreeze, however, acts on a single axis.


Useful function thank you.

Just a minor comment: After using cbfreeze on a Horizontal colorbar, the 'XTickLabelMode' property becomes set to 'manual' (differently than the default property which is set to 'auto'. Setting it to manual makes the XTickLabels to be left-aligned and if they have different number of characters (e.g. {'-0.01', '0', '0.01'}) those with less will be not properly aligned with the centre of their XTick positions.

Making sure the 'XTickLabelMode' property keeps as 'auto' in your code should avoid this issue!


Martin Richard


Good job, thank you.
IAN COLON, try to insert the file cbhandle in your working directory.


I had the same problem as Ines and Makarand, but I used a different solution. It might even be possible to include this in cbfreeze.m

I apply cbfreeze before the label, then the width doesn't change. After adding a second axis with a second colorbar, I find the handle to the old colorbar (as the only unknown colorbar handle) and then put the label on.

for i=1:length(colorbarHandles)
if colorbarHandles(i)~=cBarAxis2
ylabel(cBarAxis1,'my label')

Ian Colon

It does not work for me. I keep getting an error:
??? Undefined function or method 'cbhandle' for input arguments of type




Dear Makarand,

I had the same issue, but managed a workaround, which hopefully works for you too.
First I control the colorbar position options so that the it is located slightly farther from the image, and looks much thinner than I want it to be; when I call cbfreeze, the command won't thicken it too much. Regarding the colorbar label, I add it at the very last moment, by deciding its location with [a,b]=ginput, and then writing it with text(a,b,'color_label').



There is little bug in cbfreeze. If we have a label to colorbar the lable vanishes after use of cbfreeze and thickness increases.
looking forward for reply

Matthias Pospiech

It does not work for me. I have two pcolor plots. If I use cbfreeze the colorbar is displaced with comlete different position values and overlapps with the second pcolor plot. The colorbar is saved, but the first pcolor plot still has the colors of the second pcolor plot. So in result everything looks messed up and the colors of the plots are not saved.

On request I can submit the plot file for testing/debugging.

David Verrelli

As I explore this further, I note another problem with the foregoing code:
while the colour scale is logarithmic, as desired, the scale on the z-axis is also logarithmic, which MAY or MAY NOT be desired.


David Verrelli

Re. my previous post, Solution ID: 1-16X5V does seem to work, and is in fact more elegant than I'd given it credit for.

A more complete example, with a practical application to surface plots (rather than just displaying a raster image) is as follows:
[X,Y,D] = peaks;
% D is your 'elevation' data

% Rescale data 1-64
%d = log10(D);
d = real( log10(D) );
mn = min(min(d));
rng = max(max(d))-mn;
d = 1+63*(d-mn)/rng; % Self scale data
hC = colorbar;

LInput = -10:10;
L = 10.^LInput;
% Choose appropriate
% or somehow auto generate colorbar labels
l = 1+63*(log10(L)-mn)/rng; % Tick mark positions

This seems to work. The main problem is that the input data goes negative, but that is not a fault of the method per se.


David Verrelli

I have the same problem as Lachlan Hennessy.
They might help.
The latter looks like a slightly more elegant solution, but I can't get it to work, at least with surf. I suspect the only way is the clunky workaround of the first link. Or to dig around inside the colorbar function itself.

Lachlan Hennessy

cbfreeze does not seem to support log scale colorbars. I have been having some trouble with creating a contourf plot with a logarithmic scale and colorbar. I was hoping that this could be treated in a future update.

Thank you


Hi everyone, I've got a problem: when I use the function cmapping in the command line it works, but when I try to use it in a GUI there are no errors, but the function does nothing, please help me.




I use cbfreeze in combination with freezeColor and got exactly what I need!!!



There is a little bug on CMAPPING. Magenta 'm' color is colored as red 'r' in v1.0. Well...


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

Community Treasure Hunt

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

Start Hunting!