From: Patrick Xu on
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!
From: Walter Roberson on
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.
From: Yair Altman on
Walter Roberson <roberson(a)hushmail.com> wrote in message <47IHn.10680$wV.1561(a)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