title: Redrawing legend when the figure is closed
body: <p>Using matplotlib, I'm trying to execute a callback function when a figure is closed, which redraws the figure legend. However, when I call ax.legend(), it seems to block any further code being executed. So in the code below, 'after' is never printed. </p>
  
  <p>Could someone explain why this is? And is it possible for me to run code after the legend() call, but before the figure closes? The ultimate goal is to save two different versions of a figure when it is closed, redrawing the legend in between saves. Thanks. </p>
  
  <pre><code>from __future__ import print_function
  import matplotlib.pyplot as plt
  
  def handle_close(evt):
      f = evt.canvas.figure
      print('Figure {0} closing'.format(f.get_label()))
      ax = f.get_axes()
  
      print('before')
      leg = ax.legend()  # This line causes a problem
      print('after')  # This line (and later) is not executed
  
  xs = range(0, 10, 1)
  ys = [x*x for x in xs]
  zs = [3*x for x in xs]
  
  fig = plt.figure('red and blue')
  ax = fig.add_subplot(111)
  
  ax.plot(xs, ys, 'b-', label='blue plot')
  ax.plot(xs, zs, 'r-', label='red plot')
  
  fig.canvas.mpl_connect('close_event', handle_close)
  ax.legend()
  plt.show()
  </code></pre>