Multiple legends on Axes in App Designer?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello,
I'm facing a problem on App Designer , I can't display 2 different legends on the axes.
I have no error message so far, the thing is, only the legend for the second declared plot (displays a maximum; l2 and h2legend) is diplqyed
Here is my code:
hold(app.UIAxes, 'on')
l1=plot(app.UIAxes,c,a); % c and a are vectors containing points coordinates
l2=plot(app.UIAxes,c,app.mx{1,2:end},'--','LineWidth', 3,'DisplayName','Maximum'); %app.m is a table in xhich I'd like to store different lines (one line for each parameter)
hold(app.UIAxes, 'off')
n=numel(app.BaseframeListBox.ItemsData);
vec=1:n;
app.UIAxes.XTick = vec; %vec is an array of integers used to place xticks
app.UIAxes.XTickLabel = app.xtik; %app.xtick is a vector of strings
h1legend=legend(app.UIAxes,app.show.Engine_Ref{:},'Location', 'southoutside','Orientation','horizontal');
hold (app.UIAxes, "on")
h2legend=legend(app.UIAxes,l2,'Max'); %,'Location', 'southoutside','Orientation','horizontal');
% h3legend=legend(app.UIAxes,[h1legend,h2legend]); % an unsuccessful test
hold (app.UIAxes, "off")
h3legend.NumColumns=3;
Could you help me please to diplay 2 different legend box?
Thanking you in advance for your help!
댓글 수: 1
Adam Danz
2021년 4월 19일
julien attiogbe's answer moved here as a comment
Hello YvesVincent,
I am just curious to know if you have found a solution to your problem? I could use this double legend.
Thanks !
Florin Postolache's answer moved here as a comment
I am also interested about double legends! Thanks.
답변 (2개)
Adam Danz
2021년 4월 19일
편집: Adam Danz
2021년 4월 19일
Only one legend can be associated with a pair of axes. When a second legend is assigned to axes, it replaces the first one.
Instead of trying to add a second legend, consider using multiple columns within a single legend (see NumColumns legend property). This is the cleaner, easier approach.
If you really want a second legend to appear you can create a second pair of invisible axes containing invisible objects that should appear in a second legend and then position the legend within the figure. Solution 2 of this answer provides a demo.
Here's another quick demo that should get you started. It stacks legend #2 under legend #1. Legend #2 follows legend #1 if the figure is resized or the axis position changes.
% Plot stuff on the main axes (app.UIAxes)
app.UIFigure = uifigure();
app.UIAxes = uiaxes(app.UIFigure);
hold(app.UIAxes, 'on')
h1 = plot(app.UIAxes, magic(5), '-o');
h2 = plot(app.UIAxes, 1:5,rand(5,5)*25,'*');
% Duplicate the axes, clear the new axes
app.UIAxes2 = copyobj(app.UIAxes,app.UIFigure);
cla(app.UIAxes2)
% Add anything from axes1 to axes2 and make the
% object(s) and axes invisible.
h2Copy = copyobj(h2, app.UIAxes2);
set(h2Copy,'Visible', 'off')
app.UIAxes2.Visible = 'off';
% Create first legend and set its location
leg1 = legend(app.UIAxes,h1,{'line1','line2','line2','line4','line5'},'Location','EastOutside');
% Create second legend and position it under the first one.
leg2 = legend(app.UIAxes2,h2,{'dot1','dot2','dot2','dot4','dot5'},'Location','EastOutside');
leg2.Position(2) = leg1.Position(2)-leg2.Position(4)
% Add listener to update position of legend2 when legend1 moves
leg1.UserData = addlistener(leg1,'MarkedClean',@(~,~)set(leg2,'Position',leg1.Position-[0,leg1.Position(4),0,0]));
댓글 수: 0
Jalaj Gambhir
2019년 9월 17일
Hi,
As I understand, you want to add multiple legends in App Designer. This can be easily done as in the example below:
hold(app.UIAxes,'on');
t = linspace(-10,10,1000);
sin_t = sin(t);
cos_t = cos(t);
plot(app.UIAxes,t,sin_t,'Color','blue');
plot(app.UIAxes,t,cos_t,'Color','r');
legend(app.UIAxes,{'y = sin(x)','y = cos(x)'},'Location','southoutside');
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!