Line marker specifiers duplicated - subplot legend

3 views (last 30 days)
I am using subplots (3X4) in a figure. I have 9 plots which means in the third row, plots 10, 11, and 12 are free. I am trying to put my legend in the space of plot 10. Could anyone explain why there are some extra symbols creeping in between (shown inside the red circle in picture)? The relevant part of my code is as follows:
subplot(3,4,10)
axis off
plot(p1, 'ks', 'MarkerEdgeColor','k', 'MarkerFaceColor','w','MarkerSize',8);
axis off
plot(p2, 'ko', 'MarkerEdgeColor','k', 'MarkerFaceColor','k','MarkerSize',5);
hold on;
axis off
plot(p3, 'k^', 'MarkerEdgeColor','k', 'MarkerFaceColor','w','MarkerSize',7);
hold on;
axis off
plot(p4, 'k^', 'MarkerEdgeColor','k', 'MarkerFaceColor','k','MarkerSize',7);
hold on;
axis off
plot(p5, '-k', 'MarkerEdgeColor','k', 'MarkerFaceColor','k','MarkerSize',2);
hold on;
axis off
plot(p6, '--k', 'MarkerEdgeColor','k', 'MarkerFaceColor','k','MarkerSize',2);
hold on;
legend(...
'abc',...
'abc',...
'abc',...
'abc',...
'abc',...
'abc',...
'Location', 'North');
legend('boxoff')
title('Legend')

Accepted Answer

Mike Garrity
Mike Garrity on 5 Feb 2015
Are you sure you're not just seeing your plots behind the legend? Things are pretty crowded in that tiny axes. What happens if you add this:
set(get(gca,'Children'),'Visible','off')
to hide the plots in that axes.
BTW, I don't think your code actually matches the picture. Your code doesn't do "hold on" until after you plot p2. That means that plotting p2 would have overwritten p1. And yet we can see some square markers.
  2 Comments
Vineet
Vineet on 5 Feb 2015
Hi Mike, That is great, it solved the problem. I would like to know what is happening here. What does 'Children' stand for? BTW, you were right, when I copied my and pasted, a 'hold on' after 'p1' went missing. Thank you!
Mike Garrity
Mike Garrity on 6 Feb 2015
Children is a property on the axes which holds references to all of the objects that those plot command put there. They're the same objects that plot is returning if you had said
h = plot(...)
So that code snippet says "take the results of all of the plots commands and make them invisible". That way I have an axes which contains the right set of lines to make the legend, but I can't see them.
Note that this isn't the same as
axis off
That just hides all of the axes decorations such as tick labels. It doesn't hide the children.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!