How can I show all the plotted lines in the legend?

4 次查看(过去 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.
%%
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

采纳的回答

Adam Danz
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 个评论
Wiqas Ahmad
Wiqas Ahmad 2021-1-29
Thank you for your modification and improvement to my program. The code works well but still needs some modification. I want to display the legend as Actual data(black dot), Data fit (green dot) and four PolyCurves 4,5,6,7um (r,y,b,g). Could you please have a glance again to your code? The figure diplayed by your code can be seen
Adam Danz
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 CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by