Reputation: 659
I need to plot data of two vectors, and want the data points of each to be shown in a different colour that is explaned in a legend. However, the code below displays only the legend for the second one. What am I doing wrong?
for i_plot = 1 : plot_step : N
subplot(N, 1, i_plot)
h_A = plot(bookmarksA(i_plot, :),0,'b.','MarkerSize',24);
legend('a');
xlim ([0 pieceDuration])
set(gca, 'yTick', []);
title(subj_string(i_plot,:))
hold on
h_Z = plot(bookmarksZ(i_plot, :),0,'r.','MarkerSize',24);
legend(h_Z, 'z');
end
Upvotes: 2
Views: 62
Reputation: 65430
You're only passing one label / handle combination to the legend
command at a time. For a given axes, each call to legend
overrides previous calls to legend
, deleting previous legends rather than adding to an existing legend. You'll want to call legend
once with both plot handles and labels.
legend([h_A, h_Z], {'a', 'z'})
Update
Since in your case h_A
and h_Z
are arrays of plot handles with identical appearances, you can just pass the first item from h_A
and h_Z
to legend
.
legend([h_A(1), h_Z(1)], {'a', 'z'})
Upvotes: 2