# How can I display data in order in the legend?

1 ビュー (過去 30 日間)
Wiqas Ahmad 2021 年 2 月 1 日
コメント済み: Wiqas Ahmad 2021 年 2 月 2 日
I want to display the legend as; black dot as actual data, green dot as data fit and red line starting from PolyCurve4/mum and so on. Secondly, If I want just to diplay PolyCurves without showing black dots and green dots in the plot, what modification would I need?
clc
clear all
for n=4:1:6;
filename = sprintf('data_%d.mat',n);
E(n-3,:)=EC(1,:);
d(n-3,:)=dep(9,:);
end
hold on ; % MOVE THIS OUTSIDE THE LOOP
col=['r' 'y' 'b' 'g']; % MOVE THIS OUTSIDE THE LOOP
[m,~]=size(d);
lineHandles = gobjects(1,m); % ADD THIS
for i=1:m
PP(i,:)=plot(d(i,:),E(i,:),'ok','LineWidth',2)% original data
P1(i,:) = polyfit(d(i,:),E(i,:),2);%%2 indicates the quadratic
% x_values = min(in_(1,:)): 0.001:max(x_axis(1,:));
Y= polyval(P1(i,:),d(i,:));
h1 = plot(d(i,:),Y,'*g','LineWidth',1) % fitted
Y1 = polyval(P1(i,:),d(i,:));
R1 = corrcoef(Y1,d(i,:));
%%lsline
ss = min(d(i,:)) : 0.001: max(d(i,:));
Y2 = polyval(P1(i,:),ss);
lineHandles(i) = plot(ss,Y2,'color',col(i),'LineWidth',1); % STORE HANDLE
end
% MOVE THIS OUTSIDE THE LOOP
title('Polynomial fitting','FontSize',16,'FontWeight','normal')
xlabel('Depolarisation ratio \delta_{in}','FontSize',12,'FontWeight','normal');
ylabel('Extinction coefficient \alpha{(m^-^1)}','FontSize',12,'FontWeight','normal');
ylim([0.005 0.03])
% SUPPLY HANDLES, DEFINE EACH POLY CURVE LINE
legend([PP(i,:), h1, lineHandles],...
'Actual data','Data fit',compose('PolyCurve %d\mum',(1:m+2)))

サインインしてコメントする。

### 回答 (1 件)

Walter Roberson 2021 年 2 月 1 日
With:
legend([PP(end,:), h1, lineHandles],...
["Actual data","Data fit",compose('PolyCurve %d\mum',(1:m+2))])
Without:
legend(lineHandles, compose('PolyCurve %d\mum',(1:m+2)))
##### 1 件のコメント表示非表示 なし
Wiqas Ahmad 2021 年 2 月 2 日
Thank you

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by