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
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
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]));

Jalaj Gambhir
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');
Adding multiple “legend boxes” is not supported by MATLAB
  1 个评论
yvesvincent abgrall
yvesvincent abgrall 2019-10-17
Thank you for your answer.
Unfortunately your code only displays 2 elements is a legend, not 2 seperate legend boxes...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by