Plotting legend as a separate figure

160 views (last 30 days)
Dinal Jayasekera
Dinal Jayasekera on 18 Jul 2019
Answered: Bertrand DeChant on 21 Jun 2020
Hi there,
I currently have plotted something using MATLAB and I would like to plot the legend for this first graph as a separate figure. I thought adding "figure" prior to the legend command would solve this problem but it unfortunately didn't.
  3 Comments
Walter Roberson
Walter Roberson on 18 Jul 2019
I disagree, dpb. figure() followed by legend() would search for the current axes and when it did not find it would create a new axes and make it the current axes and then legend the visible handles from that empty axes, resulting in an empty legend. gca after the figure() is not going to be the old axes.
This assumes that no handles are passed into legend(). If handles are passed in then their axes would be found, error if they are not all the same parent axes, then the legend would be created with respect to that axes no matter what the current axes or figure is.
dpb
dpb on 19 Jul 2019
Edited: dpb on 19 Jul 2019
Yeah, that's right, Walter. I should have checked to confirm whether legend would just create its parent axes or not...it does, indeed. I knew gca itself would; should have also presumed buried in the bowels of legend would be a call to gca or the equivalent of which which would have the same effect. Got too clever for own good trying to outthink ML...
Either way, OP can't do what he's trying to do since the legend object has to have an axes as a parent and it only will label objects with handles that are on that parent axes.
So your workaround flow pattern is the only alternative I see, too...

Sign in to comment.

Answers (2)

Bertrand DeChant
Bertrand DeChant on 21 Jun 2020
Walter's suggestion led me to this solution.
Make a second plot of the data you want the legend for in a separate figure(duplicate it). Make the legend, set the figure to the size of the legend then maximize the legend within the figure. I'm unsure whether you need the plotted values to be NaN as the legend is drawn over.
figure;
hold on
% Plot whatever you like
x = 1:10;
y = NaN;
plot(x, y .* x, 'DisplayName', 'y1')
plot(x, y .* x, 'DisplayName', 'y2')
% Initial values to capture the entire legend
% Should fit most modern screens
set(gcf,'Position',[0,0,1024,1024]);
% Call the legend to your choice, I used a horizontal legend here
legend_handle = legend('Orientation','horizontal');
% Set the figure Position using the normalized legend Position vector
% as a multiplier to the figure's current position in pixels
% This sets the figure to have the same size as the legend
set(gcf,'Position',(get(legend_handle,'Position')...
.*[0, 0, 1, 1].*get(gcf,'Position')));
% The legend is still offset so set its normalized position vector to
% fill the figure
set(legend_handle,'Position',[0,0,1,1]);
% Put the figure back in the middle screen area
set(gcf, 'Position', get(gcf,'Position') + [500, 400, 0, 0]);

Walter Roberson
Walter Roberson on 18 Jul 2019
Edited: Walter Roberson on 18 Jul 2019
You just might be able to copyobj the legend to the new figure and then delete the old legend but I suspect that this will not work.
Sometimes it is necessary to create legends that do not correspond exactly to graphic objects. For example one might want to legend that a particular shape means something and that a particular color means something, without having to legend all of the combinations. Or one might want to have one legend entry for each major color in a scatter plot that used the color parameter instead of having created one scatter per color.
There is a useful technique for these kinds of legends: draw and record the handles of additional objects with the combination of attributes you want to show up in the legend, but make all the coordinates nan or inf. Nonfinite data will not be rendered on screen but you can legend() the handles to get a custom legend.
In your situation you would draw these extra invisible objects in the second figure.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!