Plotting issue: wrong plotting output when number of rows equals number of columns
1 view (last 30 days)
Show older comments
I'm plotting multiple rows of the same length (66) in a for loop, and everything looks perfect until the number of rows to be plotted are exactly the same number as the columns (length) of 66, then the plot looks extremely weird (see attached)! I realized that it only happens when I plot exactly 66 traces, if I plot 65 or 67, or any other number this issue doesn't occur and the traces are nicely plotted.
What is happening and how can I solve this?
% .mat file of AllSessionIndx is attached
AniSession_C2_FirstIndx=AllSessionIndx(1);
AniSession_C2_LastIndx=AllSessionIndx(end);
Figure
% .mat file of AllTracesPerCluster is attached
plot(1:66,AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_LastIndx,1:66));
hold on
0 Comments
Answers (3)
Star Strider
on 23 Jul 2023
Plotting a square matrix may require that the orientation is also specified, for example transposing it. It depends on how the original matrix is oriented.
t = (0:50);
y = sin((1:5:numel(t)*5).'*2*pi*t/51).'./(1:5:numel(t)*5);
figure
plot(t, y)
title('Example #1')
figure
plot(t, y.')
title('Example #2')
F = openfig('WeirdFig.fig')
LD1 = load('AllSessionIndx.mat');
AllSessionIndx = LD1.AllSessionIndx
LD2 = load('AllTracesPerCluster.mat');
AllTracesPerCluster = LD2.AllTracesPerCluster
% .mat file of AllSessionIndx is attached
AniSession_C2_FirstIndx=AllSessionIndx(1);
AniSession_C2_LastIndx=AllSessionIndx(end);
figure
% .mat file of AllTracesPerCluster is attached
plot(1:66,AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_LastIndx,1:66).');
title('Transposed Dependent Variable Matrix')
The plot function will automatically choose whatever it believes to ge the correct orientation for the dependent variable matrix, although for a square matrix it may not choose correctly. It is never inappropriate to second-guess it and transpose the dependent variable matrix to get the correct result.
.
0 Comments
Voss
on 23 Jul 2023
Referring to the plot documentation - the "Multiple set of points (using matrices)" section of the table.
"If all the sets share the same x- or y-coordinates, specify the shared coordinates as a vector and the other coordinates as a matrix. The length of the vector must match one of the dimensions of the matrix. For example:
plot([1 2 3],[4 5 6; 7 8 9])
If the matrix is square, MATLAB plots one line for each column in the matrix."
In your case the x-coordinates are always the vector 1:66 and the y-coordinates are a matrix of size N-by-66, where N varies.
When N ~= 66, only the second dimension of the y matrix matches the length of the x vector, so MATLAB plots one line for each row in the matrix, which is what you want.
However, when N == 66, both dimensions of the y matrix match the length of the x vector (i.e., the matrix is square), so "MATLAB plots one line for each column in the matrix", which produces the erroneous result.
The solution is to ensure that there are always 66 rows in the matrix you plot (i.e., its size is 66-by-N instead of N-by-66), by transposing it.
load AllSessionIndx
load AllTracesPerCluster
AniSession_C2_FirstIndx=AllSessionIndx(1);
AniSession_C2_LastIndx=AllSessionIndx(end);
plot(1:66,AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_LastIndx,1:66).');
dpb
on 23 Jul 2023
Edited: dpb
on 23 Jul 2023
load AllSessionIndx
load AllTracesPerCluster
% see whos who...
whos
% and what values were set arbitrarily
AniSession_C2_FirstIndx=AllSessionIndx(1)
AniSession_C2_LastIndx=AllSessionIndx(end)
% then open the figure
openfig('WeirdFig.fig');
Guess you're going to explain what's weird; looks perfectly reasonable given what you've asked for -- you asked to plot the first 66 columns, rows 226 thru 291 of an array of 396 rows and 69 columns overall. The plot looks like there are a couple of traces that are a little bit of an outlier from the rest of the group, but I'm sure that's what the data are that were selected; it's certainly not clear what the magic number of 66 has to to with anything; what are the last three columns? Or, did you cross some magic dividing line when the number of rows reached 66 and then's when the outliers showed up? But, there are two of those it seems, not just one. Well we can only guess...
figure, subplot(2,1,1)
plot(AllTracesPerCluster(AniSession_C2_FirstIndx+1:AniSession_C2_LastIndx,1:66));
title('Try 65 traces starting one higher')
subplot(2,1,2)
plot(AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_LastIndx-1,1:66));
title('Try 65 traces ending one lower')
Well, that does look as though the last lower values went away...so, let's look at them alone...
figure, subplot(2,1,1), hold on
plot(AllTracesPerCluster(AniSession_C2_FirstIndx,1:66));
plot(AllTracesPerCluster(AniSession_C2_LastIndx,1:66));
legend('B','E','location','West')
So, it's the beginning two that are suspect it would seem, although all the first are near zero and didn't see that ...
subplot(2,1,2)
plot(AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_FirstIndx+1,1:66).');
legend('1','2','location','NorthWest')
Conclusion -- the first two traces in the group don't fit...
Oh! The last just reminded me about the possible issue that MATLAB presumes variables are by column, not by row; you should transpose the selection to ensure are interpreted that way when the dimensions are the same both direcctions...
figure
plot(AllTracesPerCluster(AniSession_C2_FirstIndx:AniSession_C2_LastIndx,1:66).');
title('66 Traces with Transposed Array (by column)')
Mayhaps that's what you really were expecting and the alternative traces weren't the real data at all...
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!