Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

legend for group of lines

Tags:

plot

matlab

I like to plot two groups of lines in the same plot. Each group has two lines with same color and I have to draw them in the order of one group after another group. I try to show legend for groups only not for lines. How can I do this? Here is a simplification of my wrong code:

plot(x1, y1, color1); hold on;
plot(x2, y2, color1); hold on;

plot(x3, y3, color2); hold on;
plot(x4, y4, color2); hold on;

legend({color1, color2})

Thanks!


UPDATE:

A new question, is there any way to write legend after each line and without overwriting the previous legend but append to it? i.e. something similar to "hold on" but applied to legend.

like image 816
Tim Avatar asked Jan 26 '10 17:01

Tim


People also ask

What is Legend () when drawing multiple curves in a figure?

Legends can be added to describe the different curves in the plot. For this, we call the legend() function after plotting the curves. This funciton adds a legend box with appropriate legends at a desired location inside the plot.

What are legends in plots?

Legends are a useful way to label data series plotted on a graph. These examples show how to create a legend and make some common modifications, such as changing the location, setting the font size, and adding a title.

How do you organize a legend in MATLAB?

By default the order of the lines in the legend is the same as they are plotted. The easiest way is to just plot them in the order you want the legend to appear. If that is not feasible, then you can change the order of the lines as they are stored in the 'Children' property of the axes.


3 Answers

There are a few ways you can do this. The easiest way is to get the handle for the first plotted line of each group and pass that as the first argument to LEGEND:

h1 = plot(x1, y1, color1);
hold on;
plot(x2, y2, color1);

h2 = plot(x3, y3, color2);
plot(x4, y4, color2);

legend([h1 h2],{'label1', 'label2'});
like image 171
gnovice Avatar answered Oct 15 '22 15:10

gnovice


You can stitch multiple lines together using NaN, which means "pick up the pen". Then the legend will treat each as a single set of data.

hold on
plot([x1 NaN x2], [y1 NaN y2], 'b');
plot([x3 NaN x4], [y3 NaN y4], 'r');
legend({'foo', 'bar'})
hold off

For convenience, you can stick this in the multi-line version of plot.

plot([x1 NaN x2], [y1 NaN y2], 'b', [x3 NaN x4], [y3 NaN y4], 'r');

This could let you set() properties for the grouped lines as units, too.

like image 30
Andrew Janke Avatar answered Oct 15 '22 16:10

Andrew Janke


In response to your update, and to extend Andrew Janke's answer, I found this method to be perfect for an automatic legend:

% Sample data
order = -1:2;      % number of orders to plot
x = (0:0.01:10)';

% Plot each instance of data in a separate graph
for i=1:numel(order)
    plot(x,besselj(order(i),x))
    hold all

    % Amend legend to include new plot
    [~,~,~,current_entries] = legend;
    legend([current_entries {sprintf('Order = %i',order(i))}]);
end

Gives the following figure: Plot of Bessel functions with automatic legend

EDIT: In Matlab 2014b, the use of "legend" has changed and the solution(s) above will throw errors. Instead, we should modify the 'String' property of the legend. Follow this code to get the same result as my previous example:

% Sample data
order = -1:2;      % number of orders to plot
x = (0:0.01:10)';

% Plot each instance of data in a separate graph
for i=1:numel(order)
    plot(x,besselj(order(i),x))
    hold on

    % Amend legend 'entries' to include new plot
    entries(i) = { sprintf('Order = %i',order(i)) };
end

% Create legend using the 'entries' strings
legend('String',entries);

Now you can add as many plots as you want and the legend will automatically update!

like image 23
PhD_Dan Avatar answered Oct 15 '22 16:10

PhD_Dan