4.91892

4.9 | 38 ratings Rate this file 227 downloads (last 30 days) File Size: 148.66 KB File ID: #7943

freezeColors / unfreezeColors

by John Iversen

 

28 Jun 2005 (Updated 16 Jul 2009)

Code covered by BSD License  

Use multiple colormaps per figure! Version 2, now works for many types of objects, not only images.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Download Now | Watch this File

File Information
Description

Version 2.3, 3/2007 (*NEW: 7/2009, Colorbar workaround is below)

Problem: There is only one colormap per figure, so all plots share the same colormap. Often one wants different colormaps.

Solution: This function pair provides an easy way to have plots using different colomaps in the same figure.

Example:
subplot(2,1,1)
imagesc(X)
colormap hot
freezeColors

subplot(2,1,2)
surf(Y)
colormap hsv
etc...

Note, you can also pass an axis handle to freezeColors. This is useful if you'd like to freeze the colors of a colorbar:

**** COLORBAR WORKAROUND ****
** Note: in recent versions of MATLAB, you must substitute
** colorbar('v6') for colorbar in the following examples
** To suppress the warning messages:
** warning('off','MATLAB:colorbar:DeprecatedV6Argument')
** you may put this command in 'startup.m' to run every time

h=colorbar;
freezeColors(h), or simply:

freezeColors(colorbar)

FUNCTIONS

freezeColors Freeze colors of plots, making them immune to later changes in the colormap. Converts colors of many graphics objects (images, surfaces, scattergroups, bar plots, patches, etc--anything using indexed CData) to [r g b] truecolor.

unfreezeColors Reverses the effects of freezeColors. Restores the original indexed color data, allowing the colormap and caxis to again affect the plots.

Demo & Documentation in freezeColors_pub.html

Acknowledgements
This submission has inspired the following:
Shaded pseudo color, imagescnan.m v2.1 (Aug 2009), COLORMAP and COLORBAR utilities (Sep 2009)
MATLAB release MATLAB 7.4 (R2007a)
Other requirements Tested under: 7.0.1, 7.3, 7.4, 2009a Newer versions must use >> colorbar('v6') to work properly
Zip File Content  
Published M Files Demonstration of freezeColors / unfreezeColors
Other Files
__MACOSX/freezeColors/._.DS_Store,
__MACOSX/freezeColors/._Contents.m,
__MACOSX/freezeColors/._freezeColors.m,
__MACOSX/freezeColors/._unfreezeColors.m,
__MACOSX/freezeColors/demo/._.DS_Store,
__MACOSX/freezeColors/demo/html/._freezeColors_pub.png,
__MACOSX/freezeColors/test/._.DS_Store,
__MACOSX/freezeColors/test/._test_main.m,
freezeColors/.DS_Store,
freezeColors/Contents.m,
freezeColors/demo/.DS_Store,
freezeColors/demo/html/freezeColors_pub.png,
freezeColors/demo/html/freezeColors_pub_01.png,
freezeColors/demo/html/freezeColors_pub_02.png,
freezeColors/freezeColors.m,
freezeColors/test/.DS_Store,
freezeColors/test/test_main.m,
freezeColors/unfreezeColors.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (54)
06 Jul 2005 PB '

Well done! This is absolutely one of my all time favourites in the file exchange. /PB

31 Aug 2005 Anil Seth

What a fine file this is.

23 Nov 2005 Anita D

really great, thank you so much, you saved my gui! now if i could only freeze the colorbars...

23 Nov 2005 Anita D

ahh, it freezes the colorbars too! took me forever to figure out, but is awesome!

18 Apr 2006 A. Brunner

What a nice tool. I was about to start writing my own little program, but why re-inventing the wheel?

Thank you for saving me time and nerves!

18 Apr 2006 O Yamashita

This is a very nice tool!!
BTW, there may be a bug on line59,
  idx = ceil( (cdata - cax(1)) / (cax(2)-cax(1)) * nColors);

when 'cdata' is of type "uint", many entries of (cdata - cax(1)) / (cax(2)-cax(1)) could be zeros. This can be avoid by changing to

idx = ceil( (cdata - cax(1))*nColors / (cax(2)-cax(1)));

Hope this helps.

19 Apr 2006 John Iversen

From the author:
A change was made to allow freezeColors to work with scaled, integer images.

Thanks to O. Yamashita for suggesting this.

The new version will appear in a few days (once reviewed by the Mathworks). In the meantime, the corrected line reads:
 idx = ceil( (double(cdata) - cax(1)) / (cax(2)-cax(1)) * nColors);

Thank you for the interest.

23 Jun 2006 Mike L

It's just what I need and does the job very nicely. Excellent script.

26 Jul 2006 Darren M

Was bashing my head off the wall trying to get a grayscale image to be beside a hot and 2 personalized colormaps for a publication. Thanks much!

05 Sep 2006 John Iversen

Note from author:
A second, updated version (2.1) uploaded 9/5/06. I realized v2 didn't freeze colors of bar plots, so that was added. Comments welcome.

15 Oct 2006 J Roering

simple, easy, works perfectly, THANKS for saving from the "CLim" script offered with the matlab help section.

26 Nov 2006 p p

Is there a version for matlab R13 ?

04 Dec 2006 Samuel Tucker

very sweet... also appears to work for R13.

17 Jan 2007 M C

Very good, useful function. Thanks.

20 Feb 2007 Anne Marie treguier  
05 Apr 2007 Gleb Tcheslavski

very handi! Thank's!

11 Apr 2007 Andrew Stevens

It is a good attempt at patching a fundamental problem in ML.

However, when I try to use it with a colorbar, the function resets the scale of the colorbar to go between 0 and 1. The same thing appears to happen in the author's demo.

Does anyone know how to get around this problem? I tried first freezing the colorbar and then the axis and vice-versa (without luck).

12 Apr 2007 John Iversen

Hi,

Thanks for the comment. My apologies. I noticed this problem as well, and it has been fixed. However, the newest version is not up yet. Please check back in a few days. In the meantime, I'm happy to email the newer version to you.

John Iversen

17 Apr 2007 Chris K

A really great tool, but I am having a problem where the colormap of the colorbar of the first subplot is reset to the colormap of the second subplot. Any suggestions?

17 Apr 2007 Chris K

Oops, apologies! I just noticed the freezeColors(colorbar) option. Now I'm satisfied!

15 May 2007 Yongmin Jiang

Another side effect is the text of the figure is going to bitmap as well(does't look good), and when you save the figure in .eps, .jpg or other format, the picture quality is not as good, and the size of the figure is big.

17 May 2007 John Iversen

Yongmin, Unfortunately, those issues sound like general issues with Matlab graphics, not freezeColors. I have not encountered such problems, but if you have an example, I can look at it to see if there is a workaround. However, you may have better luck contacting Mathworks support directly.

06 Jun 2007 Frank Bahr

This is exactly what I was looking for. Thank you, guys!

26 Jun 2007 Damien G

I can now superimpose a color pseudocolor plot on another one containing a gray-scale colormap. It works, it's great. Many thanks.

28 Jun 2007 Ryan Van Fleet

The perfect fix to my problem my multi-color-mapping problem. Very Handy. I had to comment two lines out of the freeze file that produced errors. After that worked great.

16 Aug 2007 Jiank L

thanks, great. but it seems that it can not work with `set' command, only work with 'imagesc', etc. so when plotting pic. in a for loop, we have to use multiple 'plot', but not 'set'. Maybe one can add a handle in freezeColors? :

      %set(im3,'CData',cee1);
      imagesc(cee1);
      colormap(hot);
      freezeColors;
      freezeColors(colorbar);

14 Sep 2007 Bram van Hoof

Seems like an excellent way to solve this exact same problem that i have had!

27 Sep 2007 Leo B

Great job!
Works great for plotting different plots and colormaps with hold on in the same figure and axes.

25 Jan 2008 Eduardo Gonzalez

Excellent, but it did not work on colorbars in Matlab R2007b

22 May 2008 Irian O

Excellent work!

02 Jul 2008 Carlos Adrian Vargas Aguilera

Excellent function John. I just submited a function that frozen the colorbar map too for MATLAB 2007B, you should check it out: IMAGESCNAN

Carlos

18 Jul 2008 Ting-Chieh Lin

This code is very useful. However, it seems works only for the colormap. The colorbar cannot work in Matlab 2008a.

30 Jul 2008 Frank Leone

Thanks a lot, does exactly what I wanted. An additional feature, obvious but not mentioned, that it is also usable to use multiple colormaps within one set of axes using. For example:

plot (...)
colormap (...)
freezeColors
hold on;
plot (...)
colormap (...)

It is rather slow though for big plots, but that isn't a real problem.

02 Sep 2008 David Lei

unfortunatly I got an errormessage after executing the demoprogram. I have got Matlab 6.5. Is this the problem?

-------------------------------------------
subplot(2,1,1); imagesc(peaks); colormap hot; freezeColors

subplot(2,1,2); imagesc(peaks); colormap jet; freezeColors

??? Error using ==> nargchk
Too many input arguments.

Error in ==> D:\MATLAB6p5\work\freezeColors.m (checkArgs)
On line 206 ==> error(nargchk(0,3,nargs,'struct'))

Error in ==> D:\MATLAB6p5\work\freezeColors.m
On line 75 ==> [h, nancolor] = checkArgs(varargin);

15 Oct 2008 MARSSET Bruno

We had to drape P wave velocities values over seismic amplitudes, therefore we needed two different colormaps.
Playing arount with freezeColors and Transparency did the trick !
Many thatks
Bruno MARSSET

28 Nov 2008 Jody Klymak

Yes I can't figure out how to make 2008a/b colorbars freeze either.

13 Dec 2008 Jody Klymak

To follow up: colorbar.m appears to redraw the image when the colormap is updated. There is a callback to "FigureChangedColor" that erases the old image and makes a new one. So the cdata that was set in the old colorbar is erased and replaced when colormap is called. Not sure how to get around it.

21 Jan 2009 Liam Harrington-Missin

Like Jody I have been having issues with the colorbar. I can't seem to get them to stick to selected colormaps, I'm using R2007b

22 Jan 2009 Liam Harrington-Missin

To follow up: The problem is when you change the colormap and not easily solved in the freezeColors / unfreezeColors. I have created a simple fixme to get round it which I can provide if anyone needs it.

08 Feb 2009 Anthony Lamb

Very good tool John. Unfortunately when used to have two color maps and colorbars on the same plot is seems to change my surface plot colors to those that match the OpenGL renderer.. The colorbars are ok it's just that little hick up... Any suggestions on a fix????
Oh.. Liam I'd like to get my hands on your quick fix as well..

24 Mar 2009 Sebastian Thiele

Excellent Work!

21 Apr 2009 Arnaud Laurent

Thanks, you solved my problem! Nice work.

21 Apr 2009 Alessio Rucci

Hi, I'm trying to plot in the same figure (not in different subplot of thesame image) two different data with different colorbar, but if I use freezeColor it doesen't work....

[X,Y]=meshgrid(-10:10,-20:20);
Z1=sqrt((X/10).^2+(Y/10).^2)+2;
Z2=sqrt((X/10).^2+(Y/10).^2)-2;

figure,contour3(X,Y,Z1); axis xy; colormap hsv;
freezeColors; freezeColors(colorbar);
hold on;contour3(X,Y,Z2); axis xy; colormap jet;
freezeColors; freezeColors(colorbar);

is it possible to have both the colorbars, one for each data?

Thaks

05 May 2009 Jochen Deibele

Had some problems with two axes, one image in each with a colormap with 255 entries. Using freezeColors a wrong color number is detected which results in distorted images.

adding a (h) behind colormap in line 81 and caxis in line 83 solves this, so that the lines look like:
81: cmap = colormap(h);
83: cax = caxis(h);

Nevertheless: Excellent work!
This fixes a Matlab flaw (colormap should be an image or axes property).

04 Jun 2009 Daniel Golden

Great little tool, that doesn't work on colorbars in the latest version of Matlab.

Liam, it would be great if you could post your quick fix!

21 Jun 2009 Emanuele Ruffaldi  
16 Jul 2009 John Iversen

Hi,

** WORKAROUND FOR PROBLEMS WITH COLORBARS **

I'm really sorry not to respond earlier--I had no idea about the recent posts!

There is a workaround for the problem with freezing colorbars:

     freezeColors(colorbar('v6'))

It works for me as of 2009a, so I expect it will work for other earlier versions.

To silence the warning messages,
 
    warning('off','MATLAB:colorbar:DeprecatedV6Argument')

should be run (once per session--put it in 'startup.m')

I hope this helps. Please let me know if not.

Best regards,

John

21 Jul 2009 Nazatul Naquiah Ahba

cool! helps to solve my problem! :D

11 Aug 2009 Daniel Armyr

Neat program, but I found a bug:
Running the following line:
>>image; freezeColors

gives the following error message:
??? Subscript indices must either be real positive integers or logicals.

Error in ==> freezeColors at 136
        c = cmap(idx,i);

Placing a breakpoint and looking at idx on the line in question reveals that idx consists of floating point numbers and not integers at that point.

Unfortunately, I do not have enough of an understanding of the program to present a fix.

15 Aug 2009 Pearl  
21 Aug 2009 David

Hello

I using the freezing colorbars with 'v6' as you said. But this way I cannot define the location of the colorbar. Is there any simple solution for it. If I use

hc = colorbar('location',...)
freezeColors(hc)

it doesn't work anymore as 'v6' is missing...

Any ideas?

Thanks a lot,

David

25 Sep 2009 Ian Howat

Great work!

08 Oct 2009 John Iversen

Fix for error pointed out by Daniel Armyr (11Aug): "image; freezeColors"

after the line: idx = cdata;
>insert the line: idx = floor(idx);

This will take care of the problem. Thanks for pointing it out.

09 Oct 2009 David

Hi everybody!

For people having problem with the fixing of the colorbar (not only the colormap) I suggest you the combination with cbfreeze:

colorbar('location','SouthOutside');
freezeColors;
cbfreeze

which worked fine for me!

David.

Please login to add a comment or rating.
Updates
28 Nov 2005

Additional usage info was requested for freezing a colorbar

20 Apr 2006

Allow freezeColors to correctly handle scaled integer cdata. Thanks to O. Yamashita for pointing this need out.

05 Sep 2006

New version, now works on all graphics objects, whereas before it worked only on images.

06 Sep 2006

Add handling of some standard hggroup-based plot types: bar (bargroups), scatter (scattergroups).

06 Sep 2006

Bug fix.

16 Apr 2007

Improved: Published documentation & demo.
Fixed: Respect NaN color values (transparent), preserves caxis, checks for painters mode.

16 Jul 2009

Added workaround for problem freezing colorbars

Tag Activity for this File
Tag Applied By Date/Time
annotation John Iversen 22 Oct 2008 07:51:51
customization John Iversen 22 Oct 2008 07:51:51
colormap John Iversen 22 Oct 2008 07:51:51
image John Iversen 22 Oct 2008 07:51:51
imagesc John Iversen 22 Oct 2008 07:51:51
index John Iversen 22 Oct 2008 07:51:51
figure John Iversen 22 Oct 2008 07:51:51
surface John Iversen 22 Oct 2008 07:51:51
surf John Iversen 22 Oct 2008 07:51:51
caxis John Iversen 22 Oct 2008 07:51:51
colorbar John Iversen 17 Jul 2009 10:04:29
potw Shari Freedman 24 Jul 2009 12:15:18
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com