40 views (last 30 days)

Show older comments

There are several questions on here asking why pcolor does not display the full matrix. Most answers say something along the lines of "use image/imagesc instead". And then other people jump in and say pcolor is more powerful (because it allows irregularly spaced grids). In my case, I use irregularly spaced grids to display geophysical modelling results so image/imagesc does not work. Some people suggest first interpolating your irregularly-spaced data and then using image/imagesc. But, for me, the actual x and y vectors matter a lot (e.g. the location of a given model cell is very important).

I understand that pcolor removes the right column and the top row.

My question is: Why does this happen? Is this a feature or a bug?

From the pcolor help:

"The grid covers the region X=1:n and Y=1:m, where [m,n] = size(C)."

Maybe I'm misunderstanding, but doesn't the grid cover the region X = 1:n-1 and Y=1:m-1? Why does it say it covers the full region if, in reality, it cuts off the top row and right column of the data matrix?

To me this is a bug that should be fixed and everyone seems to complain about it. But I figure there must be some deeper reason why it is not (or can't be) fixed.

Here's an example script:

%Some irregularly spaced vectors:

x = [0.5 0.8 1 1.6];

y = [1.2 1.4 2 2.2];

%Some data

r = ones(4,4); %Size = 4 by 5

r(1,1) = 3;

r(4,4) = 10;

r(2,2) = 10;

pcolor(x,y,r)

colorbar

% r(2,2) is in the correct spot

% and you can see r(1,1) as well.

% But you cannot see r(4,4) = 10

Steven Lord
on 3 Dec 2020

This is not a bug. The perceived bug is the result of the 2-dimensional fencepost error. From the description of the C input argument on the documentation page for the pcolor function:

"The values in C map colors in the colormap array to the vertices surrounding each face. The color of a face depends on the color at one of its four vertices. Of the four vertices, the one that come first in X and Y determines the color of the face. If you do not specify X and Y, MATLAB uses X=1:n and Y=1:m, where [m,n] = size(C). Because of this relationship between the vertex colors and face colors, none of the values in the last row and column of C are represented in the plot."

The values in C do not represent the colors of the faces but represent the colors of the vertices. If you look at the picture included in that input argument description the corresponding C is 3-by-3 but only the four (2-by-2) elements with arrows pointing to the faces are used in determining the colors of the pcolor plot. Similarly, if you wanted to draw a 9-by-9 Sudoku grid on a piece of paper you need to draw 10 horizontal and 10 vertical lines to fully enclose the 81 squares where numbers would be placed.

Paul
on 23 Feb 2021

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!