2
votes

I have a plot which defines line properties based on test parameters. For example, in the plot below, blue lines have a value of A=1, and red is A=2. The solid lines with dots have B=10 and dashed with Xs B=20. When I create a legend, it makes an entry for each line plotted. I would like to have a legend something like this:

[blue] A=1
[red] A=2
-. B=10
--x B=20

I have many more entries than what you see below, so this would save a lot of space. Does anyone know if this is possible in Matlab?

sample plot

UPDATE

Here's what I've tried towards Eitan's answer, to no luck.

figure(2);
plot(1:5,1:5,'b');
hold all;
plot(1:5,1:5,'r');
plot(1:5,1:5,'k.-');
plot(1:5,1:5,'kx--');
h = get(gca,'Children');
M = {'A=1','A=2','B=10','B=20'};
figure(1);
legend(h,M); % This makes the legend appear in Figure 2, but I want it in 1.
1

1 Answers

2
votes

A possible way to do it would be first storing in an array h four handles corresponding to four sample lines in the following way:

  • h(1) is the handle of a blue solid line.
  • h(2) is the handle of a red solid line.
  • h(3) is the handle of a dot-dash line.
  • h(4) is the handle of a dot-cross line.

and then feeding them into legend alongside the desired strings like so:

legend(h, 'A = 1', 'A = 2', 'B = 10', 'B = 20')

EDIT: If you cannot obtain these four handles, it is also possible to plot separately "empty" lines (having NaN values for their coordinates) that have the same graphical properties (blue, red, dot-dash and cross-dash), and then get their handles. This way they exist as lineseries, but aren't actually plotted.