Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to add legend to an area plot with line plot overlay

Subject: How to add legend to an area plot with line plot overlay

From: Patrick Xu

Date: 16 May, 2010 00:30:25

Message: 1 of 3

Hi, I am trying to create a plot with an area plot and a line plot (on the second axis). My question is how do you add a legend for both plots. Essentially, I want it to look similar to Excel when you just put the second data series on a secondary axis and show the legends together. Ideally I'd like to put the legend in one box, outside of the two plots. Any help would be greatly appreciated!

Subject: How to add legend to an area plot with line plot overlay

From: Walter Roberson

Date: 16 May, 2010 01:45:35

Message: 2 of 3

Patrick Xu wrote:
> Hi, I am trying to create a plot with an area plot and a line plot (on
> the second axis). My question is how do you add a legend for both plots.

When you call legend() you can specify objects whose legend is to be
included. The legend will be positioned on the axis(es) for those
objects, according to the parameters you specify. You would want
different positions for the two different axes. There is no direct way
to specify that you would like one legend to be "underneath" or "beside"
another, but if you were to position one first then you might be able to
  get() the legend position and work out the position from there. If I
recall correctly, each legend box is placed on a new axis, so a figure
that had two plots on different axis, and distinct legends for each,
would involve a total of four axes.

Subject: How to add legend to an area plot with line plot overlay

From: Yair Altman

Date: 16 May, 2010 05:43:13

Message: 3 of 3

Walter Roberson <roberson@hushmail.com> wrote in message <47IHn.10680$wV.1561@newsfe11.iad>...
> Patrick Xu wrote:
> > Hi, I am trying to create a plot with an area plot and a line plot (on
> > the second axis). My question is how do you add a legend for both plots.
>
> When you call legend() you can specify objects whose legend is to be
> included. The legend will be positioned on the axis(es) for those
> objects, according to the parameters you specify. You would want
> different positions for the two different axes. There is no direct way
> to specify that you would like one legend to be "underneath" or "beside"
> another, but if you were to position one first then you might be able to
> get() the legend position and work out the position from there. If I
> recall correctly, each legend box is placed on a new axis, so a figure
> that had two plots on different axis, and distinct legends for each,
> would involve a total of four axes.


legend(handles, 'Location','NorthEastOutside') will place the legend above-right your axes. All the other compass positions, inside and outside the axes, are available, as well as a 'Best' and 'BestOutside' generic location. These are all documented and supported.

There also an undocumented ability to specify a numeric location index - read the legend.m file for details (it's explained immediately below the main help section).

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us