Home > Mobile >  How to have grayscale and rainbow color plots in subplots?
How to have grayscale and rainbow color plots in subplots?

Time:10-10

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;

CodePudding user response:

You can pass the desired axes to the colormap function, and specify the color scheme (e.g., jet, gray, etc.) you want to display. The axes can be obtained from the subplot function.

Try this code:

img = imread('moon.tif');

ax1 = subplot(1,3,1);
imagesc(img);
colormap(ax1, gray);
title('moon.tif');
axis tight; axis equal;

ax2 = 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);
colormap(ax2, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax3 = subplot(3,3,3);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
colormap(ax3, gray);
title('Filtered image');
axis tight; axis equal;

ax5 = subplot(3,3,5);
sigma = 9;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
colormap(ax5, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax6 = subplot(3,3,6);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
colormap(ax6, gray);
title('Filtered image');
axis tight; axis equal;

ax8 = subplot(3,3,8);
sigma = 15;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
colormap(ax8, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax9 = subplot(3,3,9);
img_filtered = conv2(img, gaussianfilter, 'same');
image(img_filtered);
colormap(ax9, gray);
title('Filtered image');
axis tight; axis equal;
  • Related