recipes : Plotting : 2D Data : Missing pcolor data


The pcolor command is chopping off my data along two sides.


The pcolor command plots the vertices of the array as a colour plot. By default it chops off the row and column data on two edges when it plots. You can see it happening here:

myMat=rand(5) %Generate a random 5 by 5 matrix
myMat =

    0.7126    0.0499    0.3839    0.0910    0.3148
    0.3781    0.6853    0.2602    0.5643    0.7267
    0.2489    0.6203    0.8775    0.1874    0.5158
    0.2529    0.7467    0.8061    0.5317    0.7906
    0.7672    0.9773    0.4611    0.3550    0.2045

pcolor(myMat) %Only 4 by 4 are plotted. 

Only a 4 by 4 grid is plotted

There are two ways you can fix this to show all of your data. The first is to pad the matrix on two sides, thereby "pushing" your data into the plot. The second is to use the imagesc (image, in newer versions of Matlab), which plots the elements of the matrix rather than the vertices.

%use padarray to add a row and column of zeros

%use imagesc on the un-padded array
axis xy %To achieve the same plot orientation as pcolor

It's worth spending a little time reading the documentation to learn the differences between pcolor and imagesc.


In the above pair of plots, the upper right 4x4 grid of squares is the same as those shown in the first plot. The reason the colours look slightly different is because, by default, Matlab adjusts the colour scale of surface plots so that they span the range of the plotted data.


Want to continue the discussion?
Enter your comments, suggestions, or thoughts below

comments powered by Disqus