Code covered by the BSD License  

Highlights from
COLORMAP and COLORBAR utilities (Jul 2014)

4.21053

4.2 | 19 ratings Rate this file 404 Downloads (last 30 days) File Size: 67 KB File ID: #24371
image thumbnail

COLORMAP and COLORBAR utilities (Jul 2014)

by

 

08 Jun 2009 (Updated )

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

| Watch this File

File Information
Description

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:
SETCOLORMAPS -> CMJOIN
COLORBARFIT -> CBFIT
Enjoy it!
Bugs reports and suggestions will be very appreciated!

Acknowledgements

This file inspired Quiver2.M V1.2 (Nov 2009), Rgbmap Color Maps, Xunits, Yunits And Zunits.M (Jul 2014), and Rgb2hex And Hex2rgb.

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

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.

30 May 2014 Pieter

Hello,

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.

Cheers,

Pieter

24 Mar 2014 Iman

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

24 Mar 2014 Iman

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

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,

Felipe

27 Feb 2014 Carlos


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

13 Feb 2014 craq

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.

15 Jan 2014 ZZL Zhang  
20 Nov 2013 Jenny

Hi,
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!
Jenny

13 Sep 2013 Jim K  
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);

figure
subplot(121)
imagesc(a);
colormap('cool');
c1 = colorbar;
freezeColors
cbfreeze(c1);
cblabel('colorbar 1','fontsize',14);

subplot(122)
surf(b,'edgecolor','none');
colormap('hot');
c2 = colorbar;
freezeColors
cbfreeze(c2);
cblabel('colorbar 2','fontsize',14);

22 May 2013 Martina Callaghan

Thanks! Super useful.

27 Feb 2013 arnold

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.

07 Jan 2013 Jakob Sievers

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

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

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!

Thanks

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

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.

colorbarHandles=findobj(gcf,'Tag','Colorbar')
for i=1:length(colorbarHandles)
if colorbarHandles(i)~=cBarAxis2
cBarAxis1=colorbarHandles(i);
end
end
ylabel(cBarAxis1,'my label')

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

08 Jul 2011 Leslie  
02 Feb 2011 Andris  
20 Jan 2011 Ines

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

Inès

20 Nov 2010 Makarand

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.

—DIV

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:
==BEGIN CODE==
[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
%image(d);
surf(d)
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
set(hC,'Ytick',l,'YTicklabel',L);
set(gca,'Ztick',l,'ZTicklabel',L);
==END CODE==

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.

—DIV

22 Oct 2010 David Verrelli

I have the same problem as Lachlan Hennessy.
See
http://www.mathworks.com/support/solutions/en/data/1-16X5V/?solution=1-16X5V
and
http://www.mathworks.com/support/solutions/en/data/1-2H5IF9/?solution=1-2H5IF9
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.
—DIV

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

25 Aug 2009 Andrea

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.

21 Aug 2009 David  
21 Aug 2009 David

Hello!

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

THANK YOU!

David

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

Carlos

Updates
30 Jun 2009

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

20 Aug 2009

Fixed bug in CBHANDLE with colorbar handle input

02 Sep 2009

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

30 Sep 2009

Fixed some bugs on CBUNITS and CBFIT.

14 Feb 2014

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

05 Jun 2014

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

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

16 Jun 2014

Fixed 'log' scale bugs in CBUNITS.

17 Jun 2014

CBUNITS Fixed small bug with '1' unit.

03 Jul 2014

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

Contact us