how do I make a single legend for many subplots with matplotlib?


I am plotting the same type of information, but for different countries, with multiple subplots with matplotlib. That is, I have 9 plots on a 3x3 grid, all with the same for lines (of course, different values per line).

However, I have not figured out how to put a single legend (since all 9 subplots have the same lines) on the figure just once.

How do I do that?

3/23/2012 5:13:11 AM

Accepted Answer

figlegend may be what you're looking for:

Example here:

Another example:

plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. )


fig.legend( lines, labels, loc = (0.5, 0), ncol=5 )
8/16/2016 2:05:19 AM

There is also a nice function get_legend_handles_labels() you can call on the last axis (if you iterate over them) that would collect everything you need from label= arguments:

handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow