legend entries ignored after using "hold" to create plots

조회 수: 1 (최근 30일)
Glenn
Glenn 2014년 1월 28일
답변: Glenn 2014년 2월 19일
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!

채택된 답변

Glenn
Glenn 2014년 2월 19일
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.

추가 답변 (1개)

Thomas
Thomas 2014년 1월 28일
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
Glenn
Glenn 2014년 1월 28일
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!

댓글을 달려면 로그인하십시오.

카테고리

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