Figure legend editing, multiple lines and adding specific text

5 次查看(过去 30 天)
Dear Matlab users,
I create a plot in which I have six data series, float.{small,medium,large} and peg.{small,medium,large}. The legend looks as follows so far:
h=legend(h(1,:),{'Small', 'Benchmark', 'Large','Small', 'Benchmark', 'Large'});
which is almost what I want, but not quite. My question is: How can I write manually into the legend, that the first three entries belong to the group "float" and the second three to the group "peg", without affecting the handles retrieved from h(1,:)?
Best,
Philipp
  1 个评论
Star Strider
Star Strider 2014-9-10
We need more details. (A bit more of your relevant code would help.)
What do you want the legend to look like, and what does it look like now?

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2014-9-10
编辑:dpb 2014-9-15
VERY crude beginning, but the idea as I understand what you're looking for can be achieved from some clever (or not so clever, maybe, but bludgeoning of) handle graphics...
% create plot with a unseen line between two groups of two by using NaN
hL=plot([nan(10,1) rand(10,2) nan(10,1) rand(10,2)]);
% write a legend with the two group labels and the lines labels in sequence
[hLg,hObj]=legend({'Grp 1';'A';'B';'Grp 2';'A';'B'});
% turn the first line off...*7* is magic number that there are six text objects first
set(hObj(7),'visible','off')
% get the x position of the line to move the text over there to the left
xLin=get(hObj(7),'xdata');
% reposition the first text and write longer header again the 0.9 was gotten by looking
set(hObj(1),'position',[xLin(1) 0.9 0],'String','Group 1')
Repeat the above for the second line group hObj(10) for the second group. The handles will be cyclic depending on the number of groups and elements/group. In your case with two groups of three it would be four lines/group counting the hidden one.
Obviously you'll want to generalize this a little and wrap in a function but it's basically just tedium to explore the handles of the legend object to find the right ones' locations...
ADDENDUM
Went ahead and did the second header...
>> set(hObj(13),'visible','off') % remaining line segments are groups of 3
>> xLin=get(hObj(13),'xdata') % the xposition
xLin =
0.0889 0.5333
>> yLin=get(hObj(4),'position') % position of second group (4th label)
yLin =
0.0889 0.4200 0
>> set(hObj(4),'position',[xLin(1) yLin(2) 0],'String','Group 2')
ADDENDUM 2
OK, I knew it would be possible to use findobj to simplify the above reliance upon hardcoded magic numbers for the appropriate handles. While waiting for a phone call this AM I dug into the details of legend a little...
First, create the initial legend with a unique header string you can use for a locator bread crumb...
[hLg,hObj]=legend({'Header';'A';'B';'Header';'A';'B'});
Then locate the groups of handles for the line and text object for the Header entries. The key is that legend places the string associated with the line as the 'Tag' value of the line object and we already know the 'string' value of the header we used...
hLinHdr=findobj(hObj,'tag','Header'); % the lines handles
hGrp=findobj(hTxt,'string','Header'); % and the text
set(hLinHdr,'visible','off') % don't display the line
grpTitle=cellstr(num2str([1:2].','Group %d')); % build the specific labels
for i=1:length(grpTitle % and write them
set(hGrp(i),'string',grpTitle(i),'horizontal','center')
end
This is actually the "clever" instead of "bludgeoning" version... :)
Shouldn't be hard to generalize and place in a wrapper to be pretty simply used.
The one thing I note here don't have time to check on at the moment is there's a blank line between each which explains the 12 lines -- six for the legends (two groups of three) plus plus the six blank lines for each of the above six. Whether that's somehow associated with the blank line in the plot or just how legend works I didn't investigate.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by