global legend for all subplots

user1934212 picture user1934212 · Aug 26, 2016 · Viewed 8.9k times · Source

I create an n x n matrix of matplot subplots which contain the same type of curve (lets name them signal1 and signal2):

n=5
f, axarr = plt.subplots(n,n)
for i,signal_generator in enumerate(signal_generators):
  y=i%n
  x=(i-y)/n
  axarr[x, y].plot(signal_generator.signal1)
  axarr[x, y].plot(signal_generator.signal2)

Since the 2 signals in each subplot each represent the same types, I want to use a figure global legend with the two entries 'signal1' 'signal2', rather then attaching the same legend to each subplot.

How would I do that?

Answer

pathoren picture pathoren · Aug 26, 2016

One way to do it is to force some extra space below the plots. Then you can fit the legend right there and have one "global" legend.

import matplotlib.pyplot as plt
import numpy as np

plt.close('all')

fig, axlist = plt.subplots(3, 3)
for ax in axlist.flatten():
    line1, = ax.plot(np.random.random(100), label='data1')
    line2, = ax.plot(np.random.random(100), label='data2')
    line3, = ax.plot(np.random.random(100), 'o', label='data3')

fig.subplots_adjust(top=0.9, left=0.1, right=0.9, bottom=0.12)  # create some space below the plots by increasing the bottom-value
axlist.flatten()[-2].legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), ncol=3)
# it would of course be better with a nicer handle to the middle-bottom axis object, but since I know it is the second last one in my 3 x 3 grid...

fig.show()

Now there will be an label below the second last (bottom middle) axis area, thanks to the bbox_to_anchor=(x, y) with a negative y-value. Depending on how many different subplots you have, and how many different lines you plot in each subplot it might be better to keep proper track of the different line-objects. Maybe append them to a list.

For me it the output figure looks like

enter image description here

Does this give you want you were looking for?