Code covered by the BSD License  

Highlights from
COLORMAP and COLORBAR utilities (Sep 2009)

4.0

4.0 | 13 ratings Rate this file 217 Downloads (last 30 days) File Size: 64.3 KB File ID: #24371
image thumbnail

COLORMAP and COLORBAR utilities (Sep 2009)

by Carlos Adrian Vargas Aguilera

 

08 Jun 2009 (Updated 30 Sep 2009)

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 - Draws a colorbar with specific color bands between its ticks.
cbfreeze - Freezes the colormap of a colorbar.
cbhandle - Handle of current colorbar axes.
cblabel - Adds a label to the colorbar.
cbunits - Adds units to the colorbar ticklabels.

I include updated versions of the following functions I submitted here previously:
SETCOLORMAPS (now called CMJOIN),
COLORBARFIT (now called CBFIT),
CBLABEL and
CBUNITS.

Enjoy it!
Bugs reports and suggestions will be very appreciated!

Acknowledgements

Usercolormap, Freeze Colors / Unfreeze Colors, Imagescnan.M V2.1 (Aug 2009), Cmapline, and Real2rgb & Colormaps inspired this file.

This file inspired Quiver2.M V1.2 (Nov 2009).

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
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.

Contact us