Matplotlib global legend for lots of subplots
<p>i have some problem figuring out how to force matplotlib to add a legend where i want. </p>

<p>The figures i currently have look like this:
<a href="http://i.stack.imgur.com/wZOKM.png" rel="nofollow"><img src="http://i.stack.imgur.com/wZOKM.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/qAES7.png" rel="nofollow"><img src="http://i.stack.imgur.com/qAES7.png" alt="enter image description here"></a></p>

<p>I generate this 2 paged plot with the following script:</p>

<pre><code>n = 1
trn = True
for i in diff.keys():
    plt.subplot(8, 4, n)
    plt.xlim([0, rounds + 1])
    plt.locator_params(axis='y', nbins=5)
    if PEPTIDE_KD[i] == 1e-2:
        plt.plot(x_cord, [x[0] for x in diff[i]], 'ro--', label='asd')
        plt.plot(x_cord, [x[1] for x in diff[i]], 'rv--', label='bsd')
    else:
        plt.plot(x_cord, [x[0] for x in diff[i]], 'bo--', label='asd')
        plt.plot(x_cord, [x[1] for x in diff[i]], 'bv--', label='csd')
    plt.title(i)
    n += 1
    if n == 33 and trn:
        n = 1
        trn = False
        plt.suptitle('Ligand energies')
        plt.locator_params(axis='y', nbins=5)
        plt.subplots_adjust(hspace=0.35)
        plt.show()

plt.legend(bbox_to_anchor=(-0.05, 1), loc=1)
plt.locator_params(axis='y', nbins=5)
plt.subplots_adjust(hspace=0.35)
plt.show()
</code></pre>

<p>However i would like to place the legend at the left side of the very first subplot, or to the right side of last subplot in the first line. </p>

<p>I can do this with an <code>if line == 0</code> bla bla, but the problem is as you can see in the plots i attached that for example the last plot only contains blue data, hence only the blue legend will show. I would like to place a full legend (red and blue) as well to the very same place. I dont even know where blue and red plot will be placed beforehand. </p>

<p>Is this possible?</p>

<p>Try using a <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figlegend" rel="nofollow">figure legend</a> which will let you create a legend for <a href="http://matplotlib.org/examples/pylab_examples/figlegend_demo.html" rel="nofollow">any of the lines plotted in</a> your figure and position it relative to the figure. </p>

python python-3.x matplotlib