Why does pcolor require you to transpose the matrix when you give it x and y vectors?
조회 수: 8 (최근 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
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
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.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!