Filter löschen
Filter löschen

Plots every datapoint instead of a line

1 Ansicht (letzte 30 Tage)
Kenneth Carlson
Kenneth Carlson am 28 Sep. 2020
Kommentiert: Adam Danz am 29 Sep. 2020
Below is pictured 3 sets of scatterplots, each with their own regresionline. The blue and red represent what I'm trying to achieve, currently the green get dot's at each datapoint of the line. If I change the order of plotting the graphs in the code, the "mistake" will jump to whatever graph is the third one plotted.
I've removed all the graphs plotted on the right axis and located the error to occour in the "yyaxis left" command, if I remove this, everything is plotted as intended. I've tried dictating the style by "LineStyle","-" with no result.
Can anyone provide me with a method of overcoming this issue
Below is supplied the skeleton program. (Un)comment "yyaxis left" to toggle the error on/off.
X_samples=(0.75:0.75:4.25); % Create x_axis for samples
x_axis = 0.75:0.03:4.5; % Create x-axis for plotting
% Sample 1
sample1_raw=[0.386 0.596 0.728 0.801 0.835]; % 5 Samples
sample1 = polyfit(X_samples,sample1_raw,4);
sample1_reg = polyval(sample1,x_axis);
% Sample2
sample2_raw=[0.7918 0.8638 0.8786 0.877 0.8689]; % 5 Samples
sample2 = polyfit(X_samples,sample2_raw,4);
sample2_reg = polyval(sample2,x_axis);
% slip
sample3_raw=[0.066 0.1407 0.208 0.28 0.368]; % 5 Samples
sample3 = polyfit(X_samples,sample3_raw,4);
sample3_reg = polyval(sample3,x_axis);
% plotting
figure('Name','Samples')
yyaxis left
hold on
xlim([0.6 4.5])
ylim([0 1])
plot(x_axis,sample2_reg,'color','b',"LineStyle","-")
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg,'color','r',"LineStyle","-")
scatter(X_samples,sample3_raw,'r')
plot(x_axis,sample1_reg,'color','g',"LineStyle","-")
scatter(X_samples,sample1_raw,'g')

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 28 Sep. 2020
Bearbeitet: Ameer Hamza am 28 Sep. 2020
This behavior is quite strange. A workaround is to set the marker type to none explicitly
plot(x_axis,sample1_reg,'color','g',"LineStyle","-", "Marker", "none")
An alternative is to directly specify the linespec input of plot()
plot(x_axis,sample2_reg,'b-')
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg,'r-')
scatter(X_samples,sample3_raw,'r')
plot(x_axis,sample1_reg,'g-')
scatter(X_samples,sample1_raw,'g')
  3 Kommentare
Ameer Hamza
Ameer Hamza am 28 Sep. 2020
I am glad to be of help! :)
Yes, I think it is a bug in the implementation of the plot. It seems to be some edge case that was not correctly tested. You may consider filing a bug report: https://www.mathworks.com/support/bugreports/report_bug
Kenneth Carlson
Kenneth Carlson am 29 Sep. 2020
Thanks I've filed a report on the link you provided.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 28 Sep. 2020
Bearbeitet: Adam Danz am 29 Sep. 2020
The problem occurs when
  1. yyaxis is used (remove yyaxis and example provided by OP behaves as expected).
  2. The full linespec is not explicitly defined in the 3rd input of plot(X,Y,LineSpec)
  3. The default LineStyle for the current line object contains a marker.
The default LineStyle order for a plot is below. Note that styles 5,6,7 contain markers but styles 1,2,3,4 are just line styles.
ax = gca();
ax.LineStyleOrder
ans =
7×2 char array
'- ' % 1
'--' % 2
': ' % 3
'-.' % 4
'o-' % 5 (error)
'^-' % 6 (error)
'*-' % 7 (error)
For whatever reason, with yyaxes the marker objects defined in LineStyles 5,6,7 are added to the plot unless a different LineSpec is explicitly assigned using the 3rd input of plot(X,Y,LineSpec) rather than using name-value pairs to assign the LineStyle.
If the LineStyleOrderIndex or the LineStyleOrder are changed to avoid the styles that contain markers (i.e., styles in 5,6,7) the problem is avoided.
% This avoids error by resetting the LineStyleOrderIndex (r2020b)
plot(x_axis,sample2_reg,'color','b',"LineStyle","-")
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg,'color','r',"LineStyle","-")
scatter(X_samples,sample3_raw,'r')
ax = gca();
ax.LineStyleOrderIndex = 1; % <-- restart the style index
plot(x_axis,sample1_reg,'color','g',"LineStyle","-")
scatter(X_samples,sample1_raw,'g')
%========================================%
% This avoids error by redefining the LineStyleOrder
ax = gca();
ax.LineStyleOrder(5:7,:) = ax.LineStyleOrder(1:3,:) % <-- redefine defaults
plot(x_axis,sample2_reg,'color','b',"LineStyle","-")
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg,'color','r',"LineStyle","-")
scatter(X_samples,sample3_raw,'r')
plot(x_axis,sample1_reg,'color','g',"LineStyle","-")
scatter(X_samples,sample1_raw,'g')
%========================================%
% This avoids error by explicitly defining the LineSpec and keeping
% everything else the same as the original.
% (The name-val pairs are not needed but they no longer interfere)
plot(x_axis,sample2_reg, 'b-', 'color','b',"LineStyle","-")
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg, 'r-', 'color','r',"LineStyle","-")
scatter(X_samples,sample3_raw,'r')
plot(x_axis,sample1_reg, 'g-', 'color','g',"LineStyle","-")
scatter(X_samples,sample1_raw,'g')
%========================================%
% This avoids error by not using yyplot and keeping everthing else
% the same as the original.
figure('Name','Samples')
% yyaxis left
hold on
xlim([0.6 4.5])
ylim([0 1])
plot(x_axis,sample2_reg,'color','b',"LineStyle","-")
scatter(X_samples,sample2_raw,'b')
plot(x_axis,sample3_reg,'color','r',"LineStyle","-")
scatter(X_samples,sample3_raw,'r')
plot(x_axis,sample1_reg,'color','g',"LineStyle","-")
scatter(X_samples,sample1_raw,'g')
  2 Kommentare
Kenneth Carlson
Kenneth Carlson am 29 Sep. 2020
I've reported it as a bug, and linked to this thread.
Adam Danz
Adam Danz am 29 Sep. 2020
Thanks, Kenneth.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by