title: Combining fig.legend with subfigures in matplotlib
body: <p><strong>Disclaimer:</strong> I am aware that using subfigures is irrelevant in this simple example, the latter is used only to show my problem: I want to be able to use <code>fig.legend()</code> with <code>fig.subfigures1</code>.</p>
  <hr />
  <p>I am currently discovering the new <a href="https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html" rel="nofollow noreferrer">subfigure</a> module of matplotlib. I noticed that figure legends created using <code>fig.legend()</code> does not show up when the main figure contains subfigures:</p>
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  
  x = np.linspace(1, 10, 10)
  y1 = x
  y2 = -x
  
  fig = plt.figure(constrained_layout=True)
  subfigs = fig.subfigures(nrows=2, ncols=1)
  
  for subfig in subfigs:
      axarr = subfig.subplots(1, 2)
      for ax in axarr.flatten():
          l1, = ax.plot(x, y1, label='line1')
          l2, = ax.plot(x, y2, label='line2')
              #
          ax.legend()
      # subfig.legend(handles=[l1, l2], loc='upper center', ncol=2)
  fig.legend(handles=[l1, l2], loc='upper center', ncol=2)
  plt.savefig('subfigures_figlegend.png', dpi=200)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/vegGH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vegGH.png" alt="enter image description here" /></a></p>
  <p>Note how that figure legend is absent. For comparison, note that is shows up when using only <code>plt.subplots</code>:</p>
  <pre><code>fig, axarr = plt.subplots(2, 2, constrained_layout=True)
  for ax in axarr.flatten():
      l1, = ax.plot(x, y1, label='line1')
      l2, = ax.plot(x, y2, label='line2')
      #
      ax.legend()
  fig.legend(handles=[l1, l2], loc='upper center', ncol=2)
  plt.savefig('subplots_figlegend.png', dpi=200)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/OjY9R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OjY9R.png" alt="enter image description here" /></a></p>