How to add two different lengend boxes for 'yyaxis' plot ?
조회 수: 3 (최근 30일)
이전 댓글 표시
I need to have to legend boxes with different legend titles, each for left and right plots. How to to do it???
close all; clc; clear;
beta = [1.0 1.25 1.5 1.75 2.0];
yyaxis left
FORWARD_MCS = [920 1560 2240 2960 3840];
plot(beta,FORWARD_MCS,'-k>','linewidth',1.3,'markersize',4,'MarkerEdgeColor','k','MarkerFaceColor','k')
hold on
FORWARD_FORM = [920 1600 2320 3000 3640];
plot(beta,FORWARD_FORM,'-bo','linewidth',1.3,'markersize',4,'MarkerEdgeColor','b','MarkerFaceColor','b')
INVERSE_FORM = [892.6491 1590.9784 2291.6563 2979.8789 3640.4997];
plot(beta,INVERSE_FORM,'-rs','linewidth',1.3,'markersize',4,'MarkerEdgeColor','r','MarkerFaceColor','r')
INVERSE_MCS = [1045.3262 1631.8083 1994.1204 2556.5234 3556.1911];
plot(beta,INVERSE_MCS,'-gd','linewidth',1.3,'markersize',4,'MarkerEdgeColor','g','MarkerFaceColor','g')
legend({'FMC','RIA','PMA','PSA'},'location','best')
xlim([1 2])
ylim([500 10000])
xticks([1 1.25 1.5 1.75 2])
xlabel('Target Reliability Index (\beta_{target})','FontWeight','Bold')
ylabel('Support Force T(kN)','FontWeight','Bold')
set(findobj(gcf,'type','axes'),'FontWeight','Bold');
yyaxis right
FORWARD_MCS = [23*10000 39*10000 56*10000 74*10000 96*10000];
plot(beta,FORWARD_MCS,'--k>','linewidth',1.3,'markersize',4,'MarkerEdgeColor','k','MarkerFaceColor','k')
hold on
FORWARD_FORM = [91*4 91*5 91*6 91*8 91*10];
plot(beta,FORWARD_FORM,'--bo','linewidth',1.3,'markersize',4,'MarkerEdgeColor','b','MarkerFaceColor','b')
INVERSE_FORM = [8 9 10 14 26];
plot(beta,INVERSE_FORM,'--rs','linewidth',1.3,'markersize',4,'MarkerEdgeColor','r','MarkerFaceColor','r')
INVERSE_MCS = [1*10000 1*10000 1*10000 1*10000 1*10000];
plot(beta,INVERSE_MCS,'--gd','linewidth',1.3,'markersize',4,'MarkerEdgeColor','g','MarkerFaceColor','g')
legend({'FMC','RIA','PMA','PSA'},'location','best','NumColumns',2)
xlim([1 2])
ylim([0 1000*10000])
xticks([1 1.25 1.5 1.75 2])
ylabel('No of models','FontWeight','Bold')
set(gca, 'YScale', 'log')
set(findobj(gcf,'type','axes'),'FontWeight','Bold');
댓글 수: 2
dpb
2020년 3월 4일
Can't this way...there's only one legend per axes and there's only one axes extant here...illustration
>> figure
>> yyaxis left
>> x=linspace(1,10);y=sin(3*x);z=y.*exp(0.5*x);
>> hL=plot(x,y,'b-');
>> yyaxis right
>> hL(2)=plot(x,z,'-r');
>> hF=gcf; % ok, now to the spelunking have created plot w/ left/right...
>> findobj(hF,'type','axes') % find the axes object(s) on figure
ans =
Axes with properties:
YAxisLocation: 'right'
YAxis: [2×1 NumericRuler]
YLim: [-150 100]
XLim: [1 10]
XScale: 'linear'
YScale: 'linear'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
>> whos ans % -- there's only one, not two...
Name Size Bytes Class Attributes
ans 1x1 8 matlab.graphics.axis.Axes
>> hAx=gca; % save the current axes in variable
>> yyaxis left % move to left being active instead of right
>> gca==hAx % is this new or same axes object? "TRUE" show is same
ans =
logical
1
>>
Well, even using plotyy doesn't let you do it easily...behind the scenes legend overwrites the other one even if try
figure
clear hAx
hAx=plotyy(x,y,x,z); % same data, two axes objects
>> hAx(1)==hAx(2) % now they aren't the same object...
ans =
logical
0
>>
hLg(1)=legend(hAx(1),'Left','Location','southwest'); % first legend
hLg(2)=legend(hAx(2),'Right','Location','northwest'); % try second on other axes
But, even so after the above
>> hLg(1)=legend(hAx(1),'Left','Location','southwest');
>> hLg(1)
ans =
Legend (Left) with properties:
String: {'Left'}
Location: 'southwest'
Orientation: 'vertical'
FontSize: 9
Position: [0.1494 0.1349 0.1286 0.0476]
Units: 'normalized'
Show all properties
>> hLg(2)=legend(hAx(2),'Right','Location','northwest');
>> hLg(1)==hLg(2)
ans =
logical
1
>> hLg(1)
ans =
Legend (Right) with properties:
String: {'Right'}
Location: 'northwest'
Orientation: 'vertical'
FontSize: 9
Position: [0.1494 0.8508 0.1429 0.0476]
Units: 'normalized'
Show all properties
>>
So it's actually that there's only one allowed for the figure it seems, not the axes as we have two demonstrably different axes here, still.
dpb
2020년 3월 4일
So, looks like without a whole lot of effort that I don't know if can ever actually there there or not, you'll be forced to label all the lines on the one legend object. Perhaps don't recognize that you can save plot handles and label all the lines on the one -- identifying which are on which axes as part of the title, maybe?
답변 (1개)
dpb
2020년 3월 5일
Just label 'em all...the line types will show up...wait until are done. Comment out the two legend() calls you have and put the following at the end (or anywhere after you've drawn all eight lines)
legend(hL,{'FMC','RIA','PMA','PSA','FMC','RIA','PMA','PSA'},'location','northwest','NumColumns',2)
You'll get the names and the two columns with solid lines for the one set and dashed for the other.
That may still be a little confusing so then add instead:
hLg=legend(hL,{'FMC','RIA','PMA','PSA','FMC','RIA','PMA','PSA'},'location','northwest','NumColumns',2);
hLg.Title.String='LH Axis RH Axis';
hLg.FontSize=8;
Salt to taste... :)
댓글 수: 2
dpb
2020년 3월 5일
As always, glad to help; particularly when are little details like this of what is/isn't supported...often leads to valid requests for enchancements or issues to raise re: "quality of implementation". The idea of a legend per axis is recurring with some cleaner way for legend to work a legitimate issue.
If this does at least provide a useful workaround for your problem, would be agoodthing™ to go ahead and Accept the Answer so others can know the issue is resolved if nothing else...
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!