Showing posts with label imagesc. Show all posts
Showing posts with label imagesc. Show all posts

Monday, December 23, 2013

How to rectify imagesc() axis in Matlab?

In Matlab, imagesc() and pcolor() are among the most used 2D plotting commands. For a given 2D matri, when you use the imagesc() command the y-axis comes inverted with respect to pcolor() command. To fix this issue and bring the y-axis increasing in positive y direction (with respect to screen plane), simply using axis xy is sufficient. Here is a small code snippet:


%% Difference between pcolor and imagesc
A=[ 1  2  3  4 7;
    5  6  7  8 -1; 
    9 10 11 12 3;
    2 3 4  6 7;
    9 10 11 12 3;
    1 2 7 -1 10;
    ];

figure (1); clf; set(gcf,'Color',[1 1 1]);
subplot(1,3,1);
pcolor(A); colorbar;
title('{\bf pcolor}(A)'); axis image;

subplot(1,3,2);
imagesc(A); colorbar;
title('{\bf imagesc}(A)'); axis image;

subplot(1,3,3);
imagesc(A); colorbar;
title('{\bf imagesc}(A) with {\bf axis xy}'); axis image;
axis xy
 


- For an earlier post on the difference of imagesc() and pcolor(), check here.
- Another post discusses the inclusion of grid lines to imagesc() here.

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/

Wednesday, December 14, 2011

Difference between pcolor() and imagesc() in Matlab

A simple matrix is plotted using both pcolor() and imagesc() commands in Matlab. And the difference between them are shown in the following figure:

% Difference between pcolor and imagesc
A=[ 1  2  3  4;
    5  6  7  8;
    9 10 11 12;];
figure (1); clf; set(gcf,'Color',[1 1 1]);
subplot(1,2,1);
pcolor(A); colorbar;
title('pcolor(A)')
subplot(1,2,2);
imagesc(A); colorbar;
title('imagesc(A)')


Basically, pcolor() does not show the last column and row of the matrix. Although the original matrix is 3x4, the pcolor() plot shows 2x3 submatrix.