Documentation Center

  • Trial Software
  • Product Updates

One Legend Entry for Group of Objects

This example shows how to group a set of lines together into one legend entry.

Plot four sine waves and four cosine waves. Return the line handles in array p.

figure
hold on

t = linspace(0,2*pi,100);
p(1) = plot(t,sin(t),'b');
p(2) = plot(t,sin(t+1/7),'b');
p(3) = plot(t,sin(t+2/7),'b');
p(4) = plot(t,sin(t+3/7),'b');

p(5) = plot(t,cos(t),'g');
p(6) = plot(t,cos(t+1/7),'g');
p(7) = plot(t,cos(t+2/7),'g');
p(8) = plot(t,cos(t+3/7),'g');
hold off % reset hold state to off

Create two hggroups and return their handles, g1 and g2.

g1 = hggroup;
g2 = hggroup;

Group all the sine plot lines together by setting their Parent property to g1. Group all the cosine plot lines together by setting their Parent property to g2. Then, add a legend with one description for each group of lines.

set(p(1:4),'Parent',g1)
set(p(5:8),'Parent',g2)
legend([g1,g2],'sine','cosine')

The legend contains one entry for each group of lines.

See Also

| | |

Was this topic helpful?