Can Subplots have own colormaps?

28 ビュー (過去 30 日間)
Roger Breton
Roger Breton 2023 年 6 月 14 日
回答済み: DGM 2023 年 6 月 14 日
I must be missing something obvious.. but I can get each subplots colorbars to plot with its own colormap? Here is my code:
RGB = imread('FleursLilac.png');
[X,cmap] = rgb2ind(RGB,32);
RGB2 = imread('Brown.jpg');
[X2,cmap2] = rgb2ind(RGB2,32);
A = reshape(1:36, 6, 6); % A = reshape(1:256, 16, 16);
subplot(1,4,1)
imshow(RGB)
title('Original RGB Image')
subplot(1,4,2);
imshow(RGB2);
title('Target Image')
colormap(cmap2);
colorbar;
subplot(1,4,3);
imagesc(A);
colormap(cmap);
colorbar;
title('Colormap A')
subplot(1,4,4);
imagesc(A);
colormap(cmap);
colorbar;
title('Colormap B')
And here is the result, in all its glory...
"Ideally", Colormap A and B should correspond to each respective images from which is was extracted.

回答 (1 件)

DGM
DGM 2023 年 6 月 14 日
Yes, but you have to be careful. By default, colormap() applies to the current figure. If you want it to apply to the current axes alone, you can specify the axes object.
RGB = imread('flowers.png');
[X,cmap] = rgb2ind(RGB,32);
RGB2 = imread('shoes.png');
[X2,cmap2] = rgb2ind(RGB2,32);
A = reshape(1:36, 6, 6); % A = reshape(1:256, 16, 16);
subplot(2,2,1)
imshow(RGB)
title('Original RGB Image')
subplot(2,2,2);
imshow(RGB2);
title('Target Image')
colormap(cmap2);
colorbar;
hax = subplot(2,2,3);
image(A);
colormap(hax,cmap);
colorbar;
title('Colormap A')
hax = subplot(2,2,4);
image(A);
colormap(hax,cmap2);
colorbar;
title('Colormap B')
Two things to note: First, I reordered the subplots so that it would be readable on the forum. Second, since the swatch charts are basically indexed color images, we should be using image() instead of imagesc() so that the colormapping is direct. Alternatively, if using imshow(), you would use something like
imshow(A,cmap)
colorbar;
instead of calling colormap() explicitly.

カテゴリ

Help Center および File ExchangeRed についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by