I am plotting the same type of information, but for different countries, with multiple subplots with Matplotlib. That is, I have nine 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 nine subplots have the same lines) on the figure just once.
How do I do that?
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')
If the pyplot
interface is being used instead of the Axes
interface, use:
handles, labels = plt.gca().get_legend_handles_labels()
To remove legends from subplots, see Remove the legend on a matplotlib figure.
To merge twinx
legends, see Secondary axis with twinx(): how to add to legend.