freezeColors / unfreezeColors

버전 2.5 (1.01 MB) 작성자: John Iversen
Use multiple colormaps per figure or axis! Works for many types of objects, not only images.
다운로드 수: 50.5K
업데이트 날짜: 2022/7/25

편집자 메모: This file was selected as MATLAB Central Pick of the Week

freezeColors/unfreezeColors

View freezeColors / unfreezeColors on File Exchange

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

icon

History

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

Usage

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

Examples

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.

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

Caveats

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 (https://github.com/jiversen/freezeColors/releases/tag/v2.5), 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!
버전 게시됨 릴리스 정보
2.5

See release notes for this release on GitHub: https://github.com/jiversen/freezeColors/releases/tag/v2.5

1.3.0.0

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.

1.1.0.0

Added workaround for problem freezing colorbars

1.0.0.0

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

이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.
이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.