# How to have grayscale and rainbow color plots in subplots?

16 views (last 30 days)
hmhuang on 9 Oct 2021
Commented: hmhuang on 9 Oct 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?
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 on 9 Oct 2021
Edited: Dave B on 9 Oct 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')
ax2=subplot(2,2,2);
surf(peaks)
colormap(ax2,'gray')
ax3=subplot(2,2,3);
surf(peaks)
colormap(ax3,'copper')
ax4=subplot(2,2,4);
surf(peaks)
colormap(ax4,'hot')
hmhuang on 9 Oct 2021
Thank you very much! That's indeed what I want!