 # One legend, two axes?

I am plotting a time series, a handful of moving averages and the
standard deviation of one of the moving averages. The first crop of
data are all in an overlapping range so are plotted using the
left-hand y axis. The standard deviation range falls way outside the
ranges of the other data streams, so I plot it on the right- hand
axis.

Since legends are associated with an axis how do I create one legend
which covers all lines in the graph? I keep getting a complaint from
mpl about the number of labels not matching the number of arrays being
plotted (one v. five if I get the legend associated with the
right-hand axis, four v. five if I get the legend associated with the
left-hand axis).

Thanks,

Skip Montanaro
skip@...789...

Skip,

You can call figlegend() and build a legend for the figure, irrespectively of any axes. With this function, you can explicitly pass it a list of the line objects and the labels.

http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=legend#matplotlib.pyplot.figlegend

I don’t think it can automatically know about all of the lines in your graph (then again, I haven’t tried and maybe it does).

I hope this helps!
Ben Root

···

On Tue, Dec 14, 2010 at 2:29 PM, Skip Montanaro <skip@…789…> wrote:

I am plotting a time series, a handful of moving averages and the

standard deviation of one of the moving averages. The first crop of

data are all in an overlapping range so are plotted using the

left-hand y axis. The standard deviation range falls way outside the

ranges of the other data streams, so I plot it on the right- hand

axis.

Since legends are associated with an axis how do I create one legend

which covers all lines in the graph? I keep getting a complaint from

mpl about the number of labels not matching the number of arrays being

plotted (one v. five if I get the legend associated with the

right-hand axis, four v. five if I get the legend associated with the

left-hand axis).

Thanks,

Skip Montanaro

skip@…789…