Why does pcolor require you to transpose the matrix when you give it x and y vectors?

13 次查看(过去 30 天)
Suppose you have an M by N matrix of data called A. Associated with that data is a vector in the x-direction with length M and a vector in the y-direction with length N. To me, the pcolor documentation suggests that pcolor(x,y,A) will give you a plot of the matrix with vertices at x and y (excluding the top row and right column). However, the pcolor documentation always uses vectors x and y with the same length, so any issues with the length of the vectors is not addressed in the documentation.
In reality, it appears you have to transpose the matrix in order for pcolor to work.
Is this a feature or a bug? Is there something obvious I am missing?
Here is an example code:
x = [0.5 0.8 1 1.6]; %Length = 4
y = [1.2 1.4 1.7 2.5 2.8]; %Length = 5
r = ones(4,5); %Size = length(x) by length(y)
r(1,1) = 3;
r(3,4) = 10;
%----> pcolor(x,y,r) %Error: Matrix dimensions must agree
pcolor(x,y,r') %Transpose works!
% Note that, even with the transpose, r(3,4) is still plotted
% in the correct location at x(3) and y(4)
  1 个评论
dpb
dpb 2020-12-3
It's owing to the convention that images are from upper left instead of lower lift ... see the note in the doc for input C
"The first vertex of a face is the one that is closest to the upper-left corner of the corresponding matrix. However, because the y-axis increases from bottom to top, the first vertex shown in the plot is typically the one in the lower-left corner of the face. To get the effect you want, you might have to change the orientation of the y-axis or the orientation of matrix C."

请先登录,再进行评论。

回答(2 个)

David Goodmanson
David Goodmanson 2020-12-3
编辑:David Goodmanson 2020-12-4
Hi Darcy.
pcolor works in the same fashion as meshgrid, and creates a grid with nx = length(x) columns and ny = length(y) rows. (That way on a plot the x variable will be across and the y variable will be up and down). So for c = r(m,q), then m = ny and q = nx.
If you were to use ndgrid instead of meshgrid, then things would match up the way you originally thought, but ndgrid is not natural for plots the way that meshgrid is.

Steven Lord
Steven Lord 2020-12-4
Suppose you have an M by N matrix of data called A. Associated with that data is a vector in the x-direction with length M and a vector in the y-direction with length N.
You have your X and Y vectors backwards. If you have a rectangular A, you need one X coordinate per column of A and one Y coordinate per row of A.
A = zeros(3, 4);
for whichX = 1:size(A, 2)
xline(whichX, 'Color', 'r', 'LineWidth', 4)
end
for whichY = 1:size(A, 1)
yline(whichY, 'Color', 'c', 'LineWidth', 4)
end
If you used pcolor to plot A, the twelve vertices would be at the intersections of the red and cyan lines. There are four red lines (at specific X coordinates) corresponding to the four columns of A and three cyan lines (at specific Y coordinates) for the three rows of A.
So transposing A would work, but I'd probably swap the first two inputs to pcolor so the first one has one element per column and the second one element per row.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by