## matplotlib-users

 [Matplotlib-users] One legend, two axes? From: Skip Montanaro - 2010-12-14 20:29:57 ```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@... ```
 Re: [Matplotlib-users] One legend, two axes? From: Benjamin Root - 2010-12-14 21:46:34 Attachments: Message as HTML ```On Tue, Dec 14, 2010 at 2:29 PM, Skip Montanaro 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@... > > > 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 ```
 Re: [Matplotlib-users] One legend, two axes? From: Skip Montanaro - 2010-12-15 12:50:20 ```> Skip,You can call figlegend() and build a legend for the figure, > irrespectively of any axes. Thanks. Sounds like exactly what I need. Skip ```
 Re: [Matplotlib-users] One legend, two axes? From: Paul Ivanov - 2010-12-16 06:00:04 ```Skip Montanaro, on 2010-12-15 12:49, wrote: > > Skip,You can call figlegend() and build a legend for the figure, > > irrespectively of any axes. > > Thanks. Sounds like exactly what I need. Hi Skip, I just wanted to chime in and give you a concrete example of how you can get all of the line instances from one axes and include them in the legend of another, or in a figlegend: ax = plt.subplot(1,2,1) ax2 = plt.subplot(1,2,2) ax.plot([0,1], label='ax1') ax2.plot([4,3], 'r--',label='ax2') lines =ax.get_lines() lines.extend(ax2.get_lines()) labels = [l.get_label() for l in lines] leg = ax.legend(lines, labels) # or, alternatively... leg2 = plt.figlegend(lines,labels,loc='center') this is a slight revision to what was previously discussed here: ; -- Paul Ivanov 314 address only used for lists, off-list direct email at: http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 ```