MATLAB: How to add a second legend-box to a figure without new plots

copy axesfigureMATLABmulitple legendsplotplottingtwo legends

Hey everyone, I already looked into the answers of the MATLAB-Community that were given to questions very similar to mine (https://de.mathworks.com/matlabcentral/answers/367161-2-legends-in-1-plot; https://de.mathworks.com/matlabcentral/answers/365006-how-to-create-2-legends-in-one-figure)
I tried these solutions, but they didn't work for me. That's why I am asking for help here.
To describe my problem, here are the facts:
  • I want to plot several functions, some are shown as solid lines and some as dotted lines
  • I added a legend to name these lines by color
  • Now I want to explain what the normal, solid lines are standing for and what the dotted-lines are standing for
  • I want to do this by adding a second legend (in an own "legend-window") to the figure
As far as I know, you can only have one legend-window for one set of axes in MATLAB, so the idea is:
  • add a second (exatly equal) set of axes to the figure
  • make this axes invisible, so you don't see it later in the plot
  • add two "helping – lines", one solid and one dotted
  • make these helping – lines also invisible
  • add a second legend to describe the helping – lines
As you can see in the following code, I made an example of how I tried to find a solution for my problem with simple functions.
It does't work good, because the second set of axes ('secondaxes') is not invisible and the second legend (the one with 'Model' and 'Catalogue' in it) is not black, but grey, because I had to make the helping lines H1 and H2 invisible…
clear all
% First Line to plot:
t = 0:0.5:5;
f = (2*sin(2*60*t));
% "Helping Lines", for example k and r:
p = 0:0.5:5;
k = 2*p + 3;
o = 0:0.5:5;
r = 2*o +3.2;
%%%%%%%%% First plot (L1) and the axes for that plot (firstax) %%%%%%%%
f1 = figure;
firstax = axes (f1, 'FontSize', 16);
hold on
xlim ([0 100]);
xlabel('Current [A]');
ylabel('Voltage [V]');
L1 = plot (f, '-', 'LineWidth', 2, 'Color', [0 84 159]/255, 'Parent', firstax);
hold off
set(firstax, 'Box', 'off')
legend(firstax, L1, '4 A', 'Location', 'southeast');
%%%%%% axes for the second plot (secondaxes) and the two helping Lines H1 and H2 %%%%%%%%
hold on
secondax = copyobj(firstax, gcf);
delete( get(secondax, 'Children'))
H1 = plot(k, '-', 'LineWidth', 2, 'Color', [0 0 0]/255, 'Parent', secondax, 'Visible', 'off');
H2 = plot(r, '--', 'LineWidth', 2, 'Color', [0 0 0]/255, 'Parent', secondax, 'Visible', 'off');
set(secondax, 'Color', 'none', 'XTick', [], 'YAxisLocation', 'right', 'Box', 'Off')
legend (secondax, [H1 H2], 'Model', 'Catalogue', 'Location', 'northeast');
hold off

Best Answer

Do you want to have two different scales on the y axes?
Modifying the placement of holds and labels seemed to fix seeing the double axes. I assume at some point you will be adding real data for H1 and H2 that won't be hidden, so that legend will appear the same. The background color can be changed by adding a Color name-value pair to the legend command.
...
%%%%%%%%% First plot (L1) and the axes for that plot (firstax) %%%%%%%%
f1 = figure;
firstax = axes (f1, 'FontSize', 16);
L1 = plot (f, '-', 'LineWidth', 2, 'Color', [0 84 159]/255, 'Parent', firstax);
set(firstax, 'Box', 'off','XLim',[0 100]);
legend(firstax, L1, {'4 A'}, 'Location', 'southeast');
%%%%%% axes for the second plot (secondaxes) and the two helping Lines H1 and H2 %%%%%%%%
hold on
secondax = copyobj(firstax, gcf);
delete( get(secondax, 'Children'))
H1 = plot(k, '-', 'LineWidth', 2, 'Color', [0 0 0]/255, 'Parent', secondax, 'Visible', 'off');
H2 = plot(r, '--', 'LineWidth', 2, 'Color', [0 0 0]/255, 'Parent', secondax, 'Visible', 'off');
set(secondax, 'Color', 'none', 'XTick', [], 'YAxisLocation', 'right', 'Box', 'Off')
legend ([H1 H2], {'Model', 'Catalogue'}, 'Location', 'northeast');
hold off
xlabel('Current [A]');
ylabel('Voltage [V]');
2legends.png