Skip to content
Snippets Groups Projects
Commit 28ad85a8 authored by Laura Nervo's avatar Laura Nervo
Browse files

freezeColors : moved toolbox into zUtil_Drawing/ folder updating the content.


Signed-off-by: default avatarLaura Nervo <laura.nervo@esrf.fr>
parent 0bab697a
No related branches found
No related tags found
No related merge requests found
% Functions enabling use of multiple colormaps per figure.
%
% version 2.3, 3/2007
%
% freezeColors - Lock colors of plot, enabling multiple colormaps per figure.
% unfreezeColors - Restore colors of a plot to original indexed color.
%
% demo/html
% freezeColors_pub.html - Published demonstration.
%
% test
% test_main - Test function.
% AUTHOR
% John Iversen, 2005-10
% john_iversen@post.harvard.edu
%
% Free for any use, so long as AUTHOR information remains with code.
%
% HISTORY
%
% JRI 6/2005 (v1)
% JRI 9/2006 (v2.1) now operates on all objects with CData (not just images as before)
% JRI 11/2006 handle NaNs in images/surfaces (was not posted on file exchange, included in v2.3)
% JRI 3/2007 (v2.3) Fix bug in handling of caxis that made colorbar scales on frozen plots incorrect.
% JRI 4/2007 Add 'published' html documentation.
% JRI 9/2010 Changed documentation for colorbars
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML is auto-generated from an M-file.
To make changes, update the M-file and republish this document.
-->
<title>Demonstration of freezeColors / unfreezeColors</title>
<meta name="generator" content="MATLAB 7.4">
<meta name="date" content="2007-04-13">
<meta name="m-file" content="freezeColors_pub"><style>
body {
background-color: white;
margin:10px;
}
h1 {
color: #990000;
font-size: x-large;
}
h2 {
color: #990000;
font-size: medium;
}
/* Make the text shrink to fit narrow windows, but not stretch too far in
wide windows. */
p,h1,h2,div.content div {
max-width: 600px;
/* Hack for IE6 */
width: auto !important; width: 600px;
}
pre.codeinput {
background: #EEEEEE;
padding: 10px;
}
@media print {
pre.codeinput {word-wrap:break-word; width:100%;}
}
span.keyword {color: #0000FF}
span.comment {color: #228B22}
span.string {color: #A020F0}
span.untermstring {color: #B20000}
span.syscmd {color: #B28C00}
pre.codeoutput {
color: #666666;
padding: 10px;
}
pre.error {
color: red;
}
p.footer {
text-align: right;
font-size: xx-small;
font-weight: lighter;
font-style: italic;
color: gray;
}
</style></head>
<body>
<div class="content">
<h1>Demonstration of freezeColors / unfreezeColors</h1>
<introduction>
<p><tt><b>freezeColors</b></tt> enables you to use multiple colormaps in a single figure.
</p>
<p>John Iversen (<a href="mailto:iversen@nsi.edu">iversen@nsi.edu</a>)
</p>
<p><img vspace="5" hspace="5" src="freezeColors_pub.png"> </p>
</introduction>
<h2>Contents</h2>
<div>
<ul>
<li><a href="#1">Problem: In MATLAB there is only one colormap per figure.</a></li>
<li><a href="#2">Solution: freezeColors</a></li>
<li><a href="#3">Usage</a></li>
<li><a href="#4">Demonstration: Plot a variety of objects using different colormaps in one figure.</a></li>
<li><a href="#5">unfreezeColors undoes the effects of freezeColors.</a></li>
<li><a href="#6">More information</a></li>
<li><a href="#7">Credits</a></li>
</ul>
</div>
<h2>Problem: In MATLAB there is only one colormap per figure.<a name="1"></a></h2>
<h2>Solution: freezeColors<a name="2"></a></h2>
<p><tt><b>freezeColors</b></tt> provides an easy solution to create plots using different colormaps in the same figure.
</p>
<p><tt><b>freezeColors</b></tt> will freeze the colors of graphics objects in the current axis so that later changes to the colormap (or caxis) will not
change the colors of these objects. Then, a new colormap can be applied to the next plot without changing the appearance of
the first axis. The original, indexed, color data is saved, and can be restored using <tt><b>unfreezeColors</b></tt>, making objects one again subject to change with the colormap.
</p>
<p><tt><b>freezeColors</b></tt> / <tt><b>unfreezeColors</b></tt> work on images, surfaces, scattergroups, bargroups, patches, etc. (Any object with CData in indexed-color mode).
</p>
<h2>Usage<a name="3"></a></h2>
<p>The basic way to do this is to follow each plot with a call to <tt><b>freezeColors</b></tt>, e.g.
</p>
<p><tt>subplot(2,1,1); imagesc(peaks); colormap hot; freezeColors</tt></p>
<p><tt>subplot(2,1,2); imagesc(peaks); colormap jet; freezeColors</tt>, etc...
</p>
<p>colorbars may be frozen using <tt>colorbar; cbfreeze</tt></p>
<p><tt><b>freezeColors</b></tt> freezes colors of all indexed-color objects in current axis. <tt><b>freezeColors(axh)</b></tt> does the same, but for objects in axis <tt>axh</tt>.
</p>
<p><tt><b>unfreezeColors</b></tt> works similarly, but unfreezes colors, restoring objects to their original state, once again subject to the colormap and
caxis.
</p>
<p><tt><b>cbfreeze</b></tt> is by Carlos Adrian Vargas Aguilera and must be downloaded separately from the fileexchange <a href="http://www.mathworks.com/matlabcentral/fileexchange/24371">here</a>.
</p>
<h2>Demonstration: Plot a variety of objects using different colormaps in one figure.<a name="4"></a></h2>
<p>Below, you will see plots using different colormaps on the same figure. Hooray! Note how after each plot, <tt><b>freezeColors</b></tt> is called, making the plot immune to subsequent changes in colormap used to affect the appearance of the next plot.
</p>
<p>The figure demonstrates the range of plots that can be used with <tt><b>freezeColors</b></tt>: images (imagesc, pcolor), surfaces (surf and surfl), scatter plots, bar plots, indeed any plot object that has CData.
</p><pre class="codeinput">figure; set(gcf,<span class="string">'color'</span>,[1 1 1])
<span class="comment">% image, colormap JET</span>
subplot(3,2,1); imagesc(peaks); axis <span class="string">xy</span>; colormap <span class="string">jet</span>; title(<span class="string">'imagesc, jet'</span>);
freezeColors <span class="comment">%freeze colors of current plot</span>
colorbar; cbfreeze <span class="comment">%how to freeze a colorbar</span>
<span class="comment">% same image, using colormap HOT</span>
subplot(3,2,2); imagesc(peaks); axis <span class="string">xy</span>; title(<span class="string">'imagesc, hot'</span>);
colormap <span class="string">hot</span> <span class="comment">%now, changing the colormap affects ONLY the current axis!</span>
freezeColors
colorbar; cbfreeze
<span class="comment">% surface</span>
subplot(3,2,3); surf(peaks); shading <span class="string">interp</span>; colormap <span class="string">hsv</span>; title(<span class="string">'surf, hsv'</span>);
freezeColors; colorbar; cbfreeze
<span class="comment">% lighted surface, with hole showing nan transparency is preserved after freezing</span>
pnan = peaks; pnan(4:8,end-7:end-3) = nan; <span class="comment">% make a small transparent patch</span>
subplot(3,2,4); surfl(pnan); shading <span class="string">interp</span>; colormap <span class="string">hot</span>; title(<span class="string">'surfl with NaNs, hot'</span>);
freezeColors; colorbar; cbfreeze
<span class="comment">% scatter plot and bar plot</span>
subplot(3,2,5); scatter(randn(100,1),randn(100,1),rand(100,1)*100,rand(100,1),<span class="string">'filled'</span>);
title(<span class="string">'scatter, cool'</span>); colormap <span class="string">cool</span>; axis(3*[-1 1 -1 1]);
freezeColors; colorbar; cbfreeze
subplot(3,2,6); bar(randn(4,3));xlim([0 5]);title(<span class="string">'bar, copper'</span>); colormap <span class="string">copper</span>;
freezeColors;
</pre><img vspace="5" hspace="5" src="freezeColors_pub_01.png"> <h2>unfreezeColors undoes the effects of freezeColors.<a name="5"></a></h2>
<p>While it is used less often, with <tt><b>unfreezeColors</b></tt> it is possible to restore a plot to its original state, meaning that it will now be subject to the current colormap. (The
original color data was stored when <tt>freezeColors</tt> was first called.
</p>
<p>Demo: If we change the colormap, then unfreeze the entire figure, all the plots will use the same colormap. This is Matlab's
standard, dreary behavior of one colormap per figure.
</p><pre class="codeinput">colormap <span class="string">gray</span>
unfreezeColors(gcf) <span class="comment">%unfreeze entire figure</span>
cbfreeze(<span class="string">'off'</span>) <span class="comment">%unfreeze all colorbars</span>
</pre><img vspace="5" hspace="5" src="freezeColors_pub_02.png"> <h2>More information<a name="6"></a></h2><pre>help freezeColors
help unfreezeColors</pre><h2>Credits<a name="7"></a></h2>
<p>Free for all uses, but please retain the following:</p><pre>Original Author:
John Iversen, 2005-10
john_iversen@post.harvard.edu</pre><pre>3/23/05</pre><p class="footer"><br>
Published with MATLAB&reg; 7.4<br></p>
</div>
</body>
</html>
\ No newline at end of file
Copyright (c) 2005, John Iversen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
% test_main Test freezeColors / unfreezeColors
%
% JRI 4/2007
% Author:
% John Iversen
% john_iversen@post.harvard.edu
disp(' ')
disp('==================================================')
disp('==freezeColors / unfreezeColors interactive test==')
disp('==================================================')
disp(' ')
disp('freezeColors allows multiple colormaps to be used per figure.')
disp(' ')
% ============================================================================ %
disp('As this is a graphics function, you will have to use your eyes to verify')
disp(' correct behavior. In addition, no errors should be reported.')
disp(' ')
disp('First, An image is drawn, using the hot colormap.')
disp(' ')
figure
set(gcf,'color',[1 1 1],'renderer','zbuffer')
% later, freezeColors will change from painters to zbuffer when it
% is called, which will change the appearance of the unfrozen plot slightly.
% This is not desired, so start out with zbuffer mode.
subplot(3,2,1)
imagesc(peaks); axis xy; colormap hot; title('colors not frozen'); h1=colorbar;
disp('>> subplot(3,2,1)')
disp('>> imagesc(peaks); axis xy; colormap hot; title(''colors not frozen''); colorbar')
% ============================================================================ %
disp(' ')
disp('Next, if we change the colormap to jet, the plot will change appearance.')
disp(' This is normal Matlab behavior.')
disp(' =Hit a key='), pause, disp(' ')
disp('>> colormap jet')
colormap jet
% ============================================================================ %
disp(' ')
disp('Now, repeat the plot, but using freezeColors. We also freeze the colorbar,')
disp(' using the function cbfreeze by Carlos Adrian Vargas Aguilera. Note: this')
disp(' function must be downloaded separately from the MATLAB Central File Exchange!')
disp(' =Hit a key='), pause, disp(' ')
subplot(3,2,2)
imagesc(peaks); axis xy; colormap jet; title('jet colormap, frozen')
freezeColors
h=colorbar;
try
cbfreeze(h1)
cbfreeze(h)
catch
disp('cbfreeze is not available, so colorbar colors will not be frozen until you')
disp(' go and download it from the file exchange. Sorry for the extra step, but')
disp(' it is necessary because of matlab internal changes. Please visit')
disp(' http://www.mathworks.com/matlabcentral/fileexchange/24371 to download it.')
freezeColors(h) %tests passing an axis handle
end
disp('>> subplot(3,2,2)')
disp('>> imagesc(p); axis xy; colormap jet; title(''jet colormap, frozen'')')
disp(' ')
disp('%% The test of freezeColors %%')
disp('>> freezeColors')
disp('>> colorbar')
disp('>> cbfreeze %NOTE: this is separately available from File Exchange')
% ============================================================================ %
disp(' ')
disp('The two plots should appear identical.')
disp(' ')
disp('However, if we now change the colormap again, this time to cool, the left')
disp(' plot should change but the frozen one on the right will remain the same.')
disp(' This shows that freezeColor is working as intended because the frozen')
disp(' plot is no longer affected by the colormap.')
disp(' =Hit a key='), pause, disp(' ')
colormap cool
disp('>> colormap cool')
% ============================================================================ %
disp(' ')
disp('Next, test some other plot types (image, scatter, bar, surf), as well as')
disp(' correct handling of NaNs in images. You will see a variety of plots,')
disp(' using a variety of different colormaps--all on the same figure.')
disp(' =Hit a key='), pause, disp(' ')
clf
subplot(3,2,1); imagesc(peaks); axis xy; colormap hsv; title('imagesc, hsv');
freezeColors; h=colorbar;
try, cbfreeze(h), catch,freezeColors(h), end
subplot(3,2,2); surf(peaks); shading interp; colormap jet; title('surf, jet');
freezeColors; h=colorbar;
try, cbfreeze(h), catch,freezeColors(h), end
subplot(3,2,3); scatter(randn(100,1),randn(100,1),rand(100,1)*100,rand(100,1),'filled');
title('scatter, cool'),colormap cool; axis(3*[-1 1 -1 1]);
freezeColors; h=colorbar;
try, cbfreeze(h), catch,freezeColors(h), end
subplot(3,2,4); bar(randn(4,4));xlim([0 5]);title('bar, copper')
colormap copper; freezeColors;
%demonstrate handling of NaNs -- ordinarily these are preserved, but there's a hidden
% option to subsitute another color for NaN
c = magic(50); c(20:30,20:30) = nan;
subplot(3,2,5); pcolor(c); shading interp; colormap hot; title('NaNs remain transparent')
view(-67,56); %so grid shows through
set(gca,'color',[0 1 0])
grid on, box off
freezeColors
subplot(3,2,6); pcolor(c); shading interp; colormap hot; title('NaN (using ''nancolor'' option)')
view(-67,56); %so grid shows through
set(gca,'color',[0 1 0])
grid on, box off
freezeColors('nancolor',[0 0 1]); %test two argument form
% ============================================================================ %
disp(' ')
disp('Finally, test unfreezeColors by unfreezing the entire figure, after')
disp(' changing colormap to gray.')
disp(' ')
disp('All plots are restored to the unfrozen state and are once again')
disp(' influenced by the colormap.')
disp(' =Hit a key='), pause, disp(' ')
colormap gray
unfreezeColors(gcf)
try
cbfreeze('off')
end
disp('>> colormap gray')
disp('>> unfreezeColors(gcf)')
disp('>> cbfreeze(''off'')')
disp(' ')
disp('Verify that all data is now displayed with the gray colormap. Now that')
disp(' the effects of freezeColors are undone, we see Matlab''s default')
disp(' single colormap per figure.')
disp(' ')
disp('==End of Test==')
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment