Plotting issue: wrong plotting output when number of rows equals number of columns

1 次查看(过去 30 天)
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

回答(3 个)

Star Strider
Star Strider 2023-7-23
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')
F =
Figure (3) with properties: Number: 3 Name: '' Color: [1 1 1] Position: [1.2457e+03 417 560 420] Units: 'pixels' Show all properties
LD1 = load('AllSessionIndx.mat');
AllSessionIndx = LD1.AllSessionIndx
AllSessionIndx = 66×1
226 227 228 229 230 231 232 233 234 235
LD2 = load('AllTracesPerCluster.mat');
AllTracesPerCluster = LD2.AllTracesPerCluster
AllTracesPerCluster = 396×69
6.4582 6.7350 7.1127 7.1649 7.2869 7.4774 7.9575 8.5971 8.5815 8.6310 8.4159 7.7608 6.9552 6.6736 6.1127 4.8636 4.7375 4.4002 3.3640 2.5223 1.1262 -0.1244 -0.7128 -1.3656 -1.8219 -1.5946 -1.9231 -2.1961 -2.4065 -3.0851 9.5928 9.4402 9.2612 9.4395 9.3146 9.2955 9.3027 9.4824 9.3555 9.6326 9.6477 9.4195 9.1450 8.8898 8.3186 7.9850 7.6116 6.5489 6.4467 6.3073 6.2604 5.7442 5.5458 5.1338 5.1620 5.0043 5.2260 4.9734 4.9859 4.8687 20.9964 20.9659 21.2023 20.9282 20.9182 21.0282 20.8715 21.0529 21.0529 20.6609 20.5755 20.9014 20.7552 19.7751 19.6273 20.0679 19.0948 18.5884 18.0788 17.0283 16.9248 16.0169 15.8174 15.5886 15.1389 15.2007 14.7649 14.8161 14.5894 14.5004 50.7791 50.9095 50.9532 51.1407 51.2376 50.6788 50.6044 50.5450 50.2657 50.3122 50.2439 50.0199 50.1256 49.4576 48.7548 47.3668 46.7976 45.2293 43.0442 41.7600 41.4289 39.8424 39.4858 39.0056 38.5186 38.3784 37.0218 36.9096 36.8473 36.8551 51.6344 51.6344 51.6640 51.5138 51.3499 51.4951 51.4891 51.4857 51.5318 51.4255 51.5289 51.5041 51.5041 51.6520 50.9434 50.9056 50.3122 49.9077 48.9568 47.6247 47.4427 47.5893 46.5217 46.3539 46.1806 46.0065 45.4967 45.2128 45.0450 45.0884 38.5979 37.3094 37.0514 37.5243 37.5936 37.7282 37.7097 37.6530 37.5729 37.4147 37.4540 37.0422 36.9637 37.0040 37.4059 36.7704 36.5528 35.8531 35.0989 34.6157 34.3530 33.0752 32.7897 33.0197 32.9536 33.0830 32.7896 32.9366 33.3360 33.5858 41.1377 41.3420 41.5965 41.4658 41.5785 41.6636 41.1582 40.9864 40.9864 41.1993 40.9408 41.0552 40.7653 40.8773 40.6130 40.0365 38.5820 38.0093 37.6229 37.2084 36.3842 35.2633 34.8770 34.9708 35.0504 36.0832 35.8314 36.0887 36.3998 36.4726 59.8603 59.7653 59.7777 59.3229 59.3456 59.4049 59.4352 59.3217 59.4473 59.6464 59.1293 59.1293 59.0335 58.0706 57.9221 57.5449 55.8496 55.0299 53.6345 52.3579 51.9614 50.6383 49.1449 48.9969 48.8194 48.5472 48.1170 48.1075 48.0968 48.1449 34.0625 34.0302 33.8037 33.4143 33.0161 32.6680 32.4513 32.4416 32.4324 32.4375 32.1975 31.7082 32.1436 31.8490 31.3342 29.8863 28.4711 26.6319 24.8887 24.0043 22.8184 22.6435 22.3218 22.1195 21.4285 21.5741 21.8032 22.0337 21.3149 21.2370 54.2540 54.2289 54.2321 54.1720 54.3839 54.0121 53.7577 53.8209 53.8185 53.7814 53.5979 53.5979 53.4357 52.9080 52.6068 51.2803 50.5350 49.9858 49.1515 48.5036 47.3702 47.0490 46.3602 46.2573 46.2412 46.2895 46.2817 46.2354 45.9277 45.9413
% .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.
.

Voss
Voss 2023-7-23
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
dpb 2023-7-23
编辑:dpb 2023-7-23
load AllSessionIndx
load AllTracesPerCluster
% see whos who...
whos
Name Size Bytes Class Attributes AllSessionIndx 66x1 528 double AllTracesPerCluster 396x69 218592 double ans 1x37 74 char cmdout 1x33 66 char
% and what values were set arbitrarily
AniSession_C2_FirstIndx=AllSessionIndx(1)
AniSession_C2_FirstIndx = 226
AniSession_C2_LastIndx=AllSessionIndx(end)
AniSession_C2_LastIndx = 291
% 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...

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by