Grouping Plot and Fill in Same legend object

10 ビュー (過去 30 日間)
curoi
curoi 2013 年 4 月 9 日
コメント済み: Arthur Vieira 2021 年 6 月 16 日
I have two plots of some mean value (hh) and two fill (he) plots of the confidence intervals for the associated mean values. I would like to be able to create only two legend entries instead of four so that the legend object is a combination of the plot and fill objects (Fill overlayed by the plot).
I've tried using hggroup to set parent figure handles to the plot and fill objects but I cannot get the legend to display the way I would like.
h1 = hggroup;
h2 = hggroup;
set(hh1, 'Parent', h1);
set(he3, 'Parent', h1);
set(hh2, 'Parent', h2);
set(he4, 'Parent', h2);
Is there a way to have overlayed legend entries for two different figure objects?
  2 件のコメント
Jonathan Epperl
Jonathan Epperl 2013 年 4 月 9 日
To clarify: You want the legend to display a little piece of a line and a filled area for each of your 2 data series?
curoi
curoi 2013 年 4 月 9 日
Yeah, so that the patch shows up with the line overlayed as a single legend entry.

サインインしてコメントする。

採用された回答

Jonathan Epperl
Jonathan Epperl 2013 年 4 月 10 日
Okay, I hope, this helps you:
% -- generate some dummy data
t = linspace(0,1);
s1 = sin(5*t);
s2 = 3-cos(5*t);
s3 = s1.*s2;
Fx = [t fliplr(t)];
Fy1 = [s1+1 fliplr(s1)-1];
Fy2 = [s2+.5 fliplr(s2)-.1];
Fy3 = [s3+.1 fliplr(s3)-.5];
% ---
% -- draw your data, save all handles
hf = fill(Fx,Fy1,'g',Fx,Fy2,'y',Fx,Fy3,'m');
hold on
hl = plot(t,s1,'r',t,s2,'b',t,s3,'c');
hleg = legend(hf,'Sin','Cos','Sin*Cos');
hold off
% The children of the legend axes are the little graphics, and the labels
hc = flipud(get(hleg,'Children'));
% For some reason, Matlab lists the legend entries in reverse order in the
% handle of the Children.
% Now we draw a line of the appropriate color over every patch in the
% legend
for ii=1:3
xp = get(hc(ii*2),'XData');
yp = get(hc(ii*2),'YData');
line(xp(2:3)+[-1 ;1]*(xp(3)-xp(2))/50,... the line will be a little longer than the box
[1 1]*(yp(2)+yp(1))/2,... and sit right in the center vertically
'Color',get(hl(ii),'Color'),... have the color of the corresponding line
'Parent',hleg,... and be drawn into the legend axes
'LineWidth',1);
end
Report back here if you need any help or explanation!
  3 件のコメント
liomsi liomsi
liomsi liomsi 2020 年 5 月 19 日
編集済み: liomsi liomsi 2020 年 5 月 19 日
with your exemple, get(hLegend,'Children') returns a 0×0 empty GraphicsPlaceholder array. Any idea why? I'm on Matlab R2017a.
Arthur Vieira
Arthur Vieira 2021 年 6 月 16 日
Same in R2020a. 'Children: [0×0 GraphicsPlaceholder]'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by