How can I show all the plotted lines in the legend?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I have obtained the figure from a program as shown below. In general, the figure contains the actual data,data fit and four polynomial curves ,4um(red),5um(yellow),6um(blue),7um(green). However, the legend just showed one line, red(4um). I want to display in the legend all other three missing lines, yellow, blue and green.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/503108/image.jpeg)
%%
clc
clear all
load data.mat
for i=1:(length(dep(1,:)))
PP(:,i)=plot(dep(:,i),ext','ok','LineWidth',2)% original data
hold on
P1(:,i) = polyfit(dep(:,i),ext,2);%%2 indicates the quadratic
% x_values = min(in_(1,:)): 0.001:max(x_axis(1,:));
Y= polyval(P1(:,i),dep(:,i));
h1 = plot(dep(:,i),Y,'*g','LineWidth',1) % fitted
hold on
Y1 = polyval(P1(:,i),dep(:,i));
R1 = corrcoef(Y1,dep(:,i));
hold on
%%lsline
ss = min(dep(:,i)) : 0.001: max(dep(:,i));
Y2 = polyval(P1(:,i),ss);
col=['r' 'y' 'b' 'g'];
h2 = plot(ss,Y2,'color',col(i),'LineWidth',1)
hold on
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');
legend('Actual data','Data fit',['Polynomial curve' num2str(i),'\mum'])
ylim([0.005 0.03])
end
댓글 수: 0
채택된 답변
Adam Danz
2021년 1월 29일
I've cleaned up your code (see inline comments).
You have to supply the legend with handles that specify what belongs in the legend and you have to store the handle from each iteration.
I've made some guesses in in the legend inputs so if it doesn't work right away, play around with it to get what you want.
hold on ; % MOVE THIS OUTSIDE THE LOOP
col=['r' 'y' 'b' 'g']; % MOVE THIS OUTSIDE THE LOOP
lineHandles = gobjects(1,length(dep(1,:))); % ADD THIS
for i=1:length(dep(1,:))
PP(:,i)=plot(dep(:,i),ext','ok','LineWidth',2)% original data
P1(:,i) = polyfit(dep(:,i),ext,2);%%2 indicates the quadratic
% x_values = min(in_(1,:)): 0.001:max(x_axis(1,:));
Y= polyval(P1(:,i),dep(:,i));
h1 = plot(dep(:,i),Y,'*g','LineWidth',1) % fitted
Y1 = polyval(P1(:,i),dep(:,i));
R1 = corrcoef(Y1,dep(:,i));
%%lsline
ss = min(dep(:,i)) : 0.001: max(dep(:,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(1,i), h1, lineHandles],...
'Actual data','Data fit',compose('PolyCurve %d\mum',1:length(dep(1,:))))
댓글 수: 2
Adam Danz
2021년 1월 29일
Try this
legend([PP(1,i), h1, lineHandles],...
[{'Actual data','Data fit'},compose('PolyCurve %d\mum',1:length(dep(1,:)))])
If that doesn't work, you need to ask yourself, is this a vector of 6 handles?
[PP(1,i), h1, lineHandles]
and is this a vector of 6 legend strings?
[{'Actual data','Data fit'},compose('PolyCurve %d\mum',1:length(dep(1,:)))]
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!