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.

1 comment:

  1. Awesome! Thanks for clearing that up. I've been looking for this simple line "axis xy" for a long time!

    ReplyDelete