title: What is the currently correct way to dynamically update plots in Jupyter/iPython?
body: <p>In the answers to <a href="https://stackoverflow.com/questions/21360361/how-to-dynamically-update-a-plot-in-a-loop-in-ipython-notebook-within-one-cell">how to dynamically update a plot in a loop in ipython notebook (within one cell)</a>, an example is given of how to dynamically update a plot inside a Jupyter notebook within a Python loop. However, this works by destroying and re-creating the plot on every iteration, and a comment in one of the threads notes that this situation can be improved by using the new-ish <code>%matplotlib nbagg</code> magic, which provides an interactive figure embedded in the notebook, rather than a static image.</p>
  
  <p>However, this wonderful new <code>nbagg</code> feature seems to be completely undocumented as far as I can tell, and I'm unable to find an example of how to use it to dynamically update a plot. Thus my question is, <strong>how does one efficiently update an existing plot in a Jupyter/Python notebook, using the nbagg backend?</strong> Since dynamically updating plots in matplotlib is a tricky issue in general, a simple working example would be an enormous help. A pointer to any documentation on the topic would also be extremely helpful.</p>
  
  <p>To be clear what I'm asking for: what I want to do is to run some simulation code for a few iterations, then draw a plot of its current state, then run it for a few more iterations, then update the plot to reflect the current state, and so on. So the idea is to draw a plot and then, without any interaction from the user, update the data in the plot without destroying and re-creating the whole thing.</p>
  
  <p>Here is some slightly modified code from the answer to the linked question above, which achieves this by re-drawing the whole figure every time. I want to achieve the same result, but more efficiently using <code>nbagg</code>.</p>
  
  <pre><code>%matplotlib inline
  import time
  import pylab as pl
  from IPython import display
  for i in range(10):
      pl.clf()
      pl.plot(pl.randn(100))
      display.display(pl.gcf())
      display.clear_output(wait=True)
      time.sleep(1.0)
  </code></pre>