Why does pcolor require you to transpose the matrix when you give it x and y vectors?
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
dpb
il 3 Dic 2020
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."
Risposte (2)
David Goodmanson
il 3 Dic 2020
Modificato: David Goodmanson
il 4 Dic 2020
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 Commenti
Steven Lord
il 4 Dic 2020
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 Commenti
Vedere anche
Categorie
Scopri di più su Discrete Data Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!