I want to draw only one legend into my figure with subplots of different numbers (from one to five depending of results). The legend should be always drawn above the first plot1. Is there a way to explicit tell matplotlib to use the first plot to draw the legend and not the last2?
I tried to calculate the positions manually in case there is more than one plot but it is not very precise and not a nice solution.
Edit: To explain a bit more. I know how to draw only one legend for many subplots (as you can see in image 2) but I want to know how to fix the legend to the first plot which was drawn and not to the last. I tried to calculate the positions depending of the plot numbers but it is not precise and sometimes looks ugly (legend comes into plot). I want to know whether I can tell matplotlib to fix the legend always to subplot1.
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
plt.figure(1, figsize=(12, 10))
nrows = 5
ncols = 1
plot_number = 1
for i in range(3):
plot = plt.subplot(nrows, ncols, plot_number)
plot_number += 1
p3 = mlines.Line2D([], [], color='r')
p4 = mlines.Line2D([], [], color='b')
plt.legend([p3, p4], ['one', 'two'],loc=4, ncol=4, mode="", borderaxespad=.5, frameon=True,
bbox_to_anchor=(0., 1.02, 1., .102))
plt.show()