Friday, December 06, 2013

Displaying grid lines in imagesc() function in Matlab


The default plot of pcolor() command in Matlab is to display the grid lines separating the each cell (top right figure below). One can use shading flat to remove the grid lines. However, for the imagesc() command, the default is not to show the grid lines (top left figure below). Below is  simple code snippet showing how to force imagesc() to have grid lines.

nx=4;
ny=4;
A=randn(nx,ny);

figure (8); clf; set(gcf,'Color',[1 1 1]);
subplot(2,2,1); 
imagesc(A); title('plain {\bf imagesc()}'); 
pbaspect([nx ny 1]);

subplot(2,2,2); 
pcolor(A);  title('plain {\bf pcolor()}')
pbaspect([nx ny 1]);

% With grid lines plotted
subplot(2,2,3:4); 
imagesc(A);
pbaspect([nx ny 1]);
title('{\bf imagesc()} with grid lines separating each cell')
set(gca,'xtick', linspace(0.5,nx+0.5,nx+1), 'ytick', linspace(0.5,ny+.5,ny+1));
set(gca,'xgrid', 'on', 'ygrid', 'on', 'gridlinestyle', '-', 'xcolor', 'k', 'ycolor', 'k');
- For an earlier post on the difference between pcolor() and imagesc(), click here.
- Note, syntax highligthing achieved by http://tohtml.com/matlab/

No comments:

Post a Comment