How to combine a marker to a line and show in legend?
26 ビュー (過去 30 日間)
古いコメントを表示
Ashvinder Singh Gill Surmaish Singh Gill
2020 年 6 月 12 日
How do I combine the markers and line plot for the case below? Example : Magenta arteisk on the green line to be shown in the legend and subsequently.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/314882/image.png)
data = xlsread("diametercyclic1.xlsx") ;
%K = data(1,[1 3 5 7]) ; % factors
%data(1,:) = [];
s1 = data(1:132,1) ; f1 = data(1:132,2) ;
s2 = data(1:38,3) ; f2 = data(1:38,4) ;
s3 = data(1:12,5) ; f3 = data(1:12,6) ;
s4 = data(1:14,7) ; f4 = data(1:14,8) ;
%s5 = data(1:18,9) ; f5 = data(1:18,10) ;
figure
hold on
axis tight
theta_max = 2* pi ;
theta_min = 0 ;
theta_4 = linspace(theta_min, theta_max,132)' ;
N_terp = 1024;
theta_2 = linspace(theta_min,theta_max,N_terp)' ;
X_terp1 = spline (theta_4, s1, theta_2);
Y_terp1 = spline (theta_4, f1,theta_2);
theta_5 = linspace(theta_min, theta_max,38)' ;
X_terp2 = spline (theta_5, s2, theta_2);
Y_terp2 = spline (theta_5, f2,theta_2);
theta_6 = linspace(theta_min, theta_max,12)' ;
X_terp3 = spline (theta_6, s3, theta_2);
Y_terp3 = spline (theta_6, f3,theta_2);
theta_7 = linspace(theta_min, theta_max,14)' ;
X_terp4 = spline (theta_7, s4, theta_2);
Y_terp4 = spline (theta_7, f4,theta_2);
%theta_8 = linspace(theta_min, theta_max,18)' ;
%X_terp5 = spline (theta_8, s4, theta_2);
%Y_terp5 = spline (theta_8, f4,theta_2);
yyaxis right
ylabel('Force (N)');
xlim([0 6])
ylim([-100 200])
plot(s1,f1,'bx')
p1 = plot(X_terp1,Y_terp1,'-r','DisplayName','d=4.5mm')
p2 = plot(nan,nan,'r-', 'DisplayName', sprintf('max=%.2f N',max(f1)))
p3 = plot(nan,nan,'r-', 'DisplayName', sprintf('min=%.2f N',min(f1)))
plot(s2,f2,'r+')
p4 = plot(X_terp2,Y_terp2,'-m','DisplayName','d=6.0mm')
p5 = plot(nan,nan,'m-', 'DisplayName', sprintf('max=%.2f N',max(f2)))
p6 = plot(nan,nan,'m-', 'DisplayName', sprintf('min=%.2f N',min(f2)))
plot(s3,f3,'m*')
p7 = plot(X_terp3,Y_terp3,'-g','DisplayName','d=7.5mm')
p8 = plot(nan,nan,'g-', 'DisplayName', sprintf('max=%.2f N',max(f3)))
p9 = plot(nan,nan,'g-', 'DisplayName', sprintf('min=%.2f N',min(f3)))
yyaxis left
xlim([0 6])
ylim([-1000 2000])
plot(s4,f4,'go')
p10 = plot(X_terp4,Y_terp4,'-b','DisplayName','d=15.0mm')
p11 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f N',max(f4)))
p12 = plot(nan,nan,'b-', 'DisplayName', sprintf('min=%.2f N',min(f4)))
%plot(s5,f5,'cs')
%p13 = plot(X_terp4,Y_terp4,'-c','DisplayName','')
%p14 = plot(nan,nan,'c-', 'DisplayName', sprintf('max=%.2f N',max(f5)))
%p15 = plot(nan,nan,'c-', 'DisplayName', sprintf('min=%.2f N',min(f5)))
grid on;
%xlim([0 0.0035])
%ylim([0 200])
ax = gca;
ax.XAxisLocation = 'origin';
title('Force vs Displacement (A)');
xlabel('Displacement (mm)');
ylabel('Force (N)');
lgd = legend([p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Diameter,d')
set(gcf,'color','w');
0 件のコメント
採用された回答
Udit Pant
2020 年 6 月 16 日
移動済み: Voss
2023 年 12 月 20 日
Hi,
This can be achieved by drawing a plot with no data (nan) with the desired line and marker attributes. This way the plot won't be visible (due to nan) but the plot handle can be added to legend.
In your code, instead of adding p1 to the legend, you can create a new plot 'ph1' and add it to the legend.
ph1 = plot(nan, nan, 'r-x', 'MarkerEdgeColor', 'b', 'DisplayName','d=4.5mm');
Similarly, you can add plots with nan data and desired line & marker attributes for other plot handles as well. Hope this helps.
1 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Scatter Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!