Just forgot, the command "legend(hgrph(1:5),'Location','best');" does work unless it is the only axes under the same figure, but the trouble arises with multiple axes objects.
How to define the parent axes for a legend object?
6 views (last 30 days)
Show older comments
Hi there everyone. My trouble is a bit nerve cracking but I've been striding for a couple of hours with no solution.
Say, I've got a full screen sized figure object with a panel on the right inside of the figure frame and two axes on the left. One of the axes has 32 semi logarithmic plots within which only 5 of those plots needs to be defined in a corresponding legend box. Each of the 32 plots are defined with indexed parents and the ones to be legended have display names defined.
Yet the trouble is, when it comes to define the axes of the object to include the legend, it falters. Yes, I did try
"legend(hgrph(1:5),'Location','best');" or
"lgnd1=legend(hAx(1),hgrph(1:5),'Location','best');" or
"lgnd1=legend(hgrph(1:5),'Location','best',hAx(1));" or
"lgnd1=legend(hgrph(1:5),'Location','best');
lgnd1.Parent=hAx(1);" sequence.
commands, but none did work. Obviously 'hAx(1)' refers to the handle of the axes I want to legend and 'hgrph(1:5)' are the handles of the semilogx plots to be legended.
All of the hgrph(1:5) handled plots have their display names defined via the corresponding semilogx commands. Remember I've said I got 32 plots parented the same axes and I just need five of them to be legended.
Best wishes to save me out of this,
5 Comments
Adam
on 5 Sep 2019
Yes, I was originally considering suggesting that there is a setting you can apply to each individual plot that tells a legend whether or not to include it, but the ability to select subsets for legends (which I think is relatively new) seemed to render this rather an over the top way of doing it.
Good that you found a way that works though.
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!