How to have grayscale and rainbow color plots in subplots?

34 Ansichten (letzte 30 Tage)
hmhuang
hmhuang am 9 Okt. 2021
Kommentiert: hmhuang am 9 Okt. 2021
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;

Akzeptierte Antwort

Dave B
Dave B am 9 Okt. 2021
Bearbeitet: Dave B am 9 Okt. 2021
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

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by