How to insert multiple legends in a figure
102 次查看(过去 30 天)
显示 更早的评论
Hi guys, I need some help.
In the above shown figure, I want to create two seperate legends. First legend to specify the type of line. i.e solid lines(some1) and dotted lines(some2) Second legend to differentiate between the colours.
Could someone help me to do this stuff. I am bit confused.
Thanks in advance Korada
2 个评论
倬睿 刘
2023-6-1
Hello, have you solved your problem? Can you tell me how it was solved? And I have another question, how is the coordinate in this picture realized? Each interval is 10 times different. I am eager to know the answer.
dpb
2023-6-1
编辑:dpb
2023-6-2
The issue of the original question on multiple legends per axes is still the same as before; MATLAB supports only a single legend object per axes. See the Answer I gave before for some ways to produce the desired effect within that immutable constraint.(*)
(*) I didn't mention there and haven't explored it to any extent, but another possibility strikes me that possibly one could use overlaying axes as is done for plotyy to put data onto the RH y-axis as well as the default LH one. OTOMH I don't recall if you can then manage to display a legend for each or not...
ADDENDUM: Well the above doesn't work, either -- even though plotyy() returns two axes handles that are overlaid on each other to appear as one; calling legend() with the second axes handle as the target creates a new legend, but it also obliterates the first -- one ends up with an array of two legend handle objects, but they're just two copies the same object; internally the fact the two axes overlay each other causes legend to not allow the second one to be attached to the second axes without deleting the first one from the first axes, first (although actually, it looks like it just copies one if it's already extant). So that thought turns out to also be a non-starter.
回答(1 个)
dpb
2016-5-28
编辑:dpb
2016-5-29
From
help legend
...
You can only display one legend per axes. ...
...
So you can't do what you've asked for specifically (which may account for the confusion part :) ). Another section notes that
"Legends for graphs that contain groups of objects such as lineseries, barseries, contourgroups, etc. created by high-level plotting commands such as plot, bar, contour, etc., by default display a single legend entry for the entire group, regardless of how many member objects it contains. However, you can customize such legends to show individual entries for all or selected member objects and assign a unique DisplayName to any of them. You control how groups appear in the legend by setting values for their Annotation and DisplayName properties with code. For information and examples about customizing legends in this manner, see Controlling Legends in the MATLAB Graphics documentation."
Following the example given there for "One Legend Entry for a Group of Objects"
hDots=semilogy(snr,some1,':'); % the dotted lines, set color as wanted per line
hold on
hLines=semilogy(snr,some1,'-'); % the solid lines, ditto color
hDotsGr = hggroup; % make HG groups out of 'em...
hLinesGr= hggroup;
set(hDots,'Parent',hDotsGr)
set(hLines,'Parent',hLinesGr)
% Include these hggroups in the legend:
set(get(get(hDotsGr,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on');
set(get(get(hLinesG,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on');
legend('Dots','Solids')
That's the basics to create the two legend entries; one for the solid the other for the dotted lines. If you need more labels, create a group for each per the other example "Grouping Objects to Reduce the Legend Entries"
ADDENDUM: I tried to make a demo using the above from the given example -- I got it to work w/o error but the ending legend was still all six lines, not the five groupings as I tried to identify. I'll see if I can debug that tonight; I've never tried this before in anger...
ADDENDUM 2:
I've not had time to delve deeply enough to "finger out" the issues with the hggroup stuff but another approach came to me...create dummy lines solely for the purpose of legend -- try the following trivial demo to see the idea:
hL=plot(randn(10,3)); % the solid lines
hold on
hD=plot(0.5*randn(10,3),':'); % and the dotted
hX=plot(nan(2,5)); % dummy that won't show 'cuz of NaN
set(hX(4:5),'color','k') % for the solid/dotted line use black
set(hX(5),'linestyle',':') % set the last one for "dotted"
legend(hX,'Blue','Green','Red','Solid','Dotted','location','best')
Now you can label the colors and the linestyles as wanted that reflect the actual plot data but you've got a given number and can associate the style as desired for the legend at the cost of just a two extra data points each for the number of lines want as legends and the task of setting their properties as desired.
I'm sure one can get the same end visual result with the hggroup stuff, but it's certainly complex to try to work through the multiple layers of the labyrinth to beat it into submission.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!