View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
COLORMAP and COLORBAR utilities (Jul 2014)

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.4 | 30 ratings Rate this file 134 Downloads (last 30 days) File Size: 67 KB File ID: #24371 Version: 1.12
image thumbnail

COLORMAP and COLORBAR utilities (Jul 2014)



08 Jun 2009 (Updated )

MATLAB color utilities including COLORMAP join and interpolation; freeze and fit COLORBAR, etc.

| Watch this File

File Information

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!


This file inspired Quiver2.M V1.2 (Nov 2009), Rgbmap Color Maps, Xunits, Yunits And Zunits.M (Jul 2014), Rgb2hex And Hex2rgb, Newcolorbar: Multiple Colormaps In The Same Axes, Easy Plot Eeg Brain Network Matlab, and Cbarrow: Pointy Ends For Colorbars.

MATLAB release MATLAB 8.2 (R2013b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (61)
07 Dec 2016 Troy

Troy (view profile)

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

06 Dec 2016 Troy

Troy (view profile)

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)

16 Nov 2016 Haibo

Haibo (view profile)

It's awesome!!! It works well!

08 Nov 2016 Johannes Rebling

I also vote for 2016 support!

Comment only
02 Nov 2016 Anneke ten Doeschate

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

14 Sep 2016 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;

09 Aug 2016 Lingxiao Zhu

I'm using 2016a(Language Chinese).

Reports XLim does not exist.

cbfreeze (line 332)
XLim = cbprops.XLim;

It happened while using cbfreeze.

Comment only
18 May 2016 Lola

Lola (view profile)

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

Comment only
09 May 2016 roy

roy (view profile)


Comment only
08 May 2016 Liu lb

Liu lb (view profile)

26 Mar 2016 dznmgy

dznmgy (view profile)

02 Nov 2015 Aneta

Aneta (view profile)

17 Sep 2015 Tanvi Gupta

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

17 Sep 2015 Tanvi Gupta

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

30 Aug 2015 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....

Comment only
25 Aug 2015 Lillian

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?

Comment only
04 Aug 2015 Tunhe

Tunhe (view profile)

26 Jun 2015 Oliver

Oliver (view profile)

corrected version:

plot command
plot command

Comment only
26 Jun 2015 Oliver

Oliver (view profile)

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.

Comment only
26 Jun 2015 Oliver

Oliver (view profile)

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.

Comment only
12 May 2015 Tristan

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

Comment only
19 Jan 2015 Manolis Oikonomakis

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

Comment only
09 Dec 2014 Karl

Karl (view profile)

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

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

Comment only
03 Sep 2014 Aurélie

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

28 Aug 2014 denny

denny (view profile)

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

05 Jun 2014 Carlos Adrian Vargas Aguilera

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.

Comment only
30 May 2014 Pieter

Pieter (view profile)


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.



Comment only
24 Mar 2014 Iman

Iman (view profile)

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,{...

Comment only
24 Mar 2014 Iman

Iman (view profile)

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.

Comment only
14 Mar 2014 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,


Comment only
27 Feb 2014 Carlos

Carlos (view profile)

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

18 Feb 2014 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.

Comment only
13 Feb 2014 craq

craq (view profile)

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.

Comment only
15 Jan 2014 ZZL Zhang  
20 Nov 2013 Jenny

Jenny (view profile)

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!

Comment only
13 Sep 2013 Jim K

Jim K (view profile)

19 Jul 2013 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);

22 May 2013 Martina Callaghan

Thanks! Super useful.

27 Feb 2013 arnold

arnold (view profile)

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

26 Jan 2013 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.

Comment only
07 Jan 2013 Jakob Sievers

Jakob Sievers (view profile)

Incredibly useful!

20 Nov 2012 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?

16 Mar 2012 carmen

carmen (view profile)

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.

28 Feb 2012 Rafa

Rafa (view profile)

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!


22 Feb 2012 Martin Richard  
30 Nov 2011 MrSeniorLoveDaddy

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

23 Nov 2011 craq

craq (view profile)

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

Comment only
20 Nov 2011 Ian Colon

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

Comment only
08 Jul 2011 Leslie

Leslie (view profile)

02 Feb 2011 Andris

Andris (view profile)

20 Jan 2011 Ines

Ines (view profile)

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


Comment only
20 Nov 2010 Makarand

Makarand (view profile)

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

19 Nov 2010 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.

26 Oct 2010 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.


Comment only
22 Oct 2010 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.


Comment only
22 Oct 2010 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.

Comment only
18 Jul 2010 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

Comment only
25 Aug 2009 Andrea

Andrea (view profile)

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.

Comment only
21 Aug 2009 David

David (view profile)

21 Aug 2009 David

David (view profile)


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



Comment only
27 Jun 2009 Carlos Adrian Vargas Aguilera

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


Comment only
30 Jun 2009 1.1

Fixed small bug with 'm' magenta color on CMAPPING. Added files versions on Contents.m

20 Aug 2009 1.2

Fixed bug in CBHANDLE with colorbar handle input

02 Sep 2009 1.3

Fixed BUGs on CBFREEZE (thanks to Sergio Muniz) and CMAPPING (thanks to Andrea Rumazza).

30 Sep 2009 1.6

Fixed some bugs on CBUNITS and CBFIT.

14 Feb 2014 1.7

Fixed bug related with colorbar title position, by Chris Rapson.

05 Jun 2014 1.8

Rewritten CB codes with new optional inputs. Fixed bugs concerned with scientific notation. Included prefix notation like 'M' instead of 'x10^6'. Not tested for log scale.

15 Jun 2014 1.9

Changed CBUNITS: Forces same decimals on labels. Scientific notation may be 'eN', '1/eN' or SI prefix.

16 Jun 2014 1.10

Fixed 'log' scale bugs in CBUNITS.

17 Jun 2014 1.11

CBUNITS Fixed small bug with '1' unit.

03 Jul 2014 1.12

Added 'upE' and 'normal' extra options for CBUNITS. Fixed bugs related with input reading (thanks to Maxime Desbiens).

Contact us