freezeColors / unfreezeColors

版本 2.5 (1.0 MB) 作者: John Iversen
Use multiple colormaps per figure or axis! Works for many types of objects, not only images.
50.1K 次下载
更新时间 2022/7/25

编者注: This file was selected as MATLAB Central Pick of the Week


View freezeColors / unfreezeColors on File Exchange

freezeColors allows multiple colormaps to be used per figure and even within a single axis.



In 2005 freezeColors was written to enable multiple colormaps in a single figure, something MATLAB could not do. It was a file exchange pick of the week in 2009.

In 2014, MATLAB enabled each axis to have its own colormap. However, freezeColors still had a following because it was more flexible and enabled multiple colormaps within a single axis as well.

As MATLAB's graphics are continually evolving, over time freezeColors stopped working in some situations, such as with colorbars. Now, in 2022, it's been brought back up to date and will hopefully work in every use people have in mind. If not, let me know and I'll try to fix it.

2022 Changes (v. 2.5)

* greater emphasis on multiple colormaps within a single axis
* new method for freezing colorbars
* scatter works properly
* works properly when calling freezeColors(axesHandle)
* includes a replacement for colorbar: jicolorbar


freezeColors            applies to all objects in current axis
freezeColors(axh)       same, but works on objects in axis axh.
freezeColors(colorbar)  creates a colorbar frozen to the current colormap


Multiple colormaps per figure (legacy)

subplot(2,1,1); imagesc(peaks); colormap hot; freezeColors; freezeColors(colorbar)
subplot(2,1,2); imagesc(peaks); colormap hsv; freezeColors; freezeColors(colorbar) %etc...

Note, in such a simple case, this could just as well use matlab's per-axis colormaps (2014 and later):

subplot(2,1,1); imagesc(peaks); colorbar; colormap(gca,'hot')
subplot(2,1,2); imagesc(peaks); colorbar; colormap(gca,'hsv')

Here is an example (from test/test_main.m) also showing one scatterplot panel using multiple colormaps in a single axis.

multiple colormaps in a figure

Interesting: multiple colormaps in an axis

freezeColors can do something that some people need and not possible currently in MATLAB: having objects using multiple different colormaps within the same axis. These examples are somewhat contrived, but illustrate the possibilities.

surf(peaks); colormap parula; freezeColors; freezeColors(jicolorbar); hold on
surf(peaks+20); caxis([14 28]); colormap gray; freezeColors; freezeColors(colorbar);
surf(peaks+40); caxis(caxis+20); colormap hot; freezeColors; freezeColors(jicolorbar('horiz'));
axis auto; shading interp; caxis([14 28]); view([-27 14]); set(gca,'color',[.8 .8 .8])

multiple colormaps in an axis

Example with surf and ribbon

second example

Example with streamribbon and coneplot

third example with streamribbon and coneplot

See test/test_main.m for more examples.

How it works

The color of some graphic objects in MATLAB is controled by a CData property (surf, image, scatter, patch, coneplot, streamribbon, etc), which is usually converted to color using the current colormap using a scaling process. Thus if the colormap changes, the appearance changes. freezeColors simply converts all graphics objects from scaled CData to direct RGB CData meaning its appearance is 'frozen' to the current colormap and thus subsequent changes to the colormap will not change its appearance. The original scaled data are stored and can be restored using unfreezeColors.


In its continued quest for improvement, some graphics objects change over time, foremost among them colorbars, which used to be implemented as images and thus could easily be frozen along with other objects. Nowadays they are encapsulated in an opaque object and we needed to find a new way to freeze them. Now simply call freezeColors(colorbar) to add a 'frozen' colorbar to an axis. It will appear to unfreeze when you change colormaps, but the next call to freezeColors will restore its frozen appearance. It will not be frozen to the caxis, so be aware of that. It's a workaround, but just remember to call freezeColors after you change the colormaps and all will be well. This has the benefit of also freezing contourf plots, as long as you call freezeColors(colorbar) after the plot.

Also, having multiple colorbars in an axis is a work in progress--a combination of jicolorbar and colorbar were used in the examples and required some attention to setting the caxis; a more sustainable solution would be a new version of jicolorbar that more easily enables multiple (frozen) colorbars per axis...


John Iversen (2024). freezeColors / unfreezeColors (, GitHub. 检索来源 .

MATLAB 版本兼容性
创建方式 R2021a
与 R2014b 及更高版本兼容
Windows macOS Linux
Help CenterMATLAB Answers 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明

See release notes for this release on GitHub:

Change documentation to suggest using cbfreeze to freeze colorbars
Fix to preserve caxis for objects within hggroups (e.g. contourf)
Updating to reflect the possible obsolescense.

Added workaround for problem freezing colorbars

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

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库