title: Matplotlib legend on first subplot
body: <p>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 plot<a href="https://i.stack.imgur.com/tiVD3.png" rel="nofollow noreferrer">1</a>. Is there a way to explicit tell matplotlib to use the first plot to draw the legend and not the last<a href="https://i.stack.imgur.com/oOhRq.png" rel="nofollow noreferrer">2</a>? </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <pre><code>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()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tiVD3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tiVD3.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/oOhRq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oOhRq.png" alt="enter image description here"></a></p>