Plots every datapoint instead of a line

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

采纳的回答

Ameer Hamza
Ameer Hamza 2020-9-28
编辑:Ameer Hamza 2020-9-28
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 个评论
Ameer Hamza
Ameer Hamza 2020-9-28
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 2020-9-29
Thanks I've filed a report on the link you provided.

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2020-9-28
编辑:Adam Danz 2020-9-29
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')

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by