How can I show all the plotted lines in the legend?
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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
0 commentaires
Réponse acceptée
  Adam Danz
    
      
 le 29 Jan 2021
        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 commentaires
  Adam Danz
    
      
 le 29 Jan 2021
				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,:)))]
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Annotations dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
