How to have grayscale and rainbow color plots in subplots?

34 ビュー (過去 30 日間)
hmhuang
hmhuang 2021 年 10 月 9 日
コメント済み: hmhuang 2021 年 10 月 9 日
I have a MATLAB code snippet that uses subplot to generate some plots in the same figure. However, I want to make surf plots in subplot(3,3,2), subplot(3,3,5), subplot(3,3,8) colorful instead of grayscale and keep subplot(1,3,1), subplot(3,3,3), subplot(3,3,6), subplot(3,3,9) grayscale. How can I achieve that?
img = imread('moon.tif');
subplot(1,3,1);
imagesc(img);
colormap(gray);
title('moon.tif');
axis tight; axis equal;
subplot(3,3,2);
sigma = 3;
gaussianfilter = fspecial('gaussian', [90,90], sigma); % typically we would choose to filter width to be six times sigma
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);
subplot(3,3,3);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
title('Filtered image');
axis tight; axis equal;
subplot(3,3,5);
sigma = 9;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);
subplot(3,3,6);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
title('Filtered image');
axis tight; axis equal;
subplot(3,3,8);
sigma = 15;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);
subplot(3,3,9);
img_filtered = conv2(img, gaussianfilter, 'same');
image(img_filtered);
title('Filtered image');
axis tight; axis equal;

採用された回答

Dave B
Dave B 2021 年 10 月 9 日
編集済み: Dave B 2021 年 10 月 9 日
You can pass in an axes to the colormap function, and you can get the axes from subplot. Just use the gray colormap for the ones you want in grayscale, and whatever non-gray colormap (or leave the default, parula) for the remainder.
ax1=subplot(2,2,1);
surf(peaks)
colormap(ax1,'turbo')
shading flat
ax2=subplot(2,2,2);
surf(peaks)
colormap(ax2,'gray')
shading flat
ax3=subplot(2,2,3);
surf(peaks)
colormap(ax3,'copper')
shading flat
ax4=subplot(2,2,4);
surf(peaks)
colormap(ax4,'hot')
shading flat
  1 件のコメント
hmhuang
hmhuang 2021 年 10 月 9 日
Thank you very much! That's indeed what I want!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by