legend entries ignored after using "hold" to create plots

6 views (last 30 days)
Glenn
Glenn on 28 Jan 2014
Answered: Glenn on 19 Feb 2014
I created a yy plot using "hold on" because my data sets were different lengths. Since then, the legend function only recognizes 4 of the 6 data sets plotted so when I add the legend it is incomplete. Can anyone tell me how to get the other entries to show up or maybe there is a better way to plot data sets with different lengths than using the "hold" function.
Thanks!

Accepted Answer

Glenn
Glenn on 19 Feb 2014
Ultimately, I handled this issue by giving up on the yy plot and did a subplot instead. I, nor a couple colleagues, was not able to get the legend to display all entries for the yyplot.

More Answers (1)

Thomas
Thomas on 28 Jan 2014
You could concatenate multiple legend statements for the two data sets
Eg.:
x = 1900:10:1990; x=x'
y1 = 10:10:100; y1=y1'
y2 = -0.1:-0.1:-1; y2=y2'
y3 = 0.05:0.05:0.5; y3=y3'
[AX,H1,H2] = plotyy(x,y1,x,[y2,y3],'plot','plot');
legend(H1,'y1');
legend(H2,'y2','y3');
this will give you only the last legend on the plot
x = 1900:10:1990; x=x'
y1 = 10:10:100; y1=y1'
y2 = -0.1:-0.1:-1; y2=y2'
y3 = 0.05:0.05:0.5; y3=y3'
[AX,H1,H2] = plotyy(x,y1,x,[y2,y3],'plot','plot');
legend([H1;H2],'y1','y2','y3');
this will give the entire data set with legend
  1 Comment
Glenn
Glenn on 28 Jan 2014
The trouble that I have is that if I cannot use the second method which shows all legend entries because my data sets are different lengths and I have more than 2 (i.e. "x" and "y" have different lengths for each iteration of the loop in which I'm calling them).
maybe seeing the actual code is helpful. The Power_XXX matricies need different lengths for each plotted curve which are dictated by the indices in "ind_DCmax"
figure(2)
%[AX,H1,H2]=plotyy(Power_final,Power_all,Power_final,[100*polyval(P,Power_AC);100*(Power_final/Power_burneroutput);
for i=1:length(T_cold)
Eff.Power_AC(:,i)=[100*polyval(P(:,i),Power_AC)];
Eff.System(:,i)=100*(Power_final'./Power_chemical(:,i))
end
Eff.all=[Eff.Power_AC,Eff.System];
%[AX,H1,H2]=plotyy(Power_final,Power_all,Power_final,Eff.all);
for i=1:length(T_cold)
[AX,H1,H2]=plotyy(Power_final(1:ind_DCmax(i)),Power_chemical(1:ind_DCmax(i),i),Power_final(1:ind_DCmax(i)),Eff.System(1:ind_DCmax(i),i));
%plot(Power_final(1:ind_DCmax(i)),Power_chemical(1:ind_DCmax(i),i),PropVal1{i},Power_final(1:ind_DCmax(i)),Power_burneroutput(1:ind_DCmax(i),i),PropVal2{i});
set(AX(1),'ylim',[0 400]);
set(AX(1),'LineStyle','-','Ytick',[0:50:400]);
set(AX(2),'ylim',[4 20]);
set(AX(2),'LineStyle','--','Ytick',[4:2:20]);
set(H1,'LineStyle','-','LineWidth',2);
set(H1,'Color',PropVal1{i}(1:1));
set(H2,'LineStyle','--','LineWidth',2);
set(H2,'Color',PropVal2{i}(1:1));
set(get(AX(2),'Ylabel'),'String','Efficiency, %');
legend([H1;H2],{sprintf('Chemical Input @T_c_o_l_d=%g°C ',T_cold(i)),sprintf('System Efficiency @T_c_o_l_d=%g°C ',T_cold(i))});
hold on
end
legend(legendtext,'Location','NorthWest');
Thank you for the help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!