title: IPython Notebook widgets for Matplotlib interactivity
body: <p>I would like to use the ipython notebook widgets to add some degree of interactivity to inline matplotlib plots. </p>
  
  <p>In general the plot can be quite heavy and I want to only update a specific element of the plot. I understand that widgets have a throttling feature built-in that helps to don't flood the kernel, but when the plot takes let say 30s I don't want to wait so long just to update a line.</p>
  
  <p>By reading the <a href="https://github.com/ipython/ipython-in-depth">example notebooks</a> I was able to create a basic example in which I add a cross cursor (driven by 2 sliders) to a mpl axis.</p>
  
  <p>The problem is that the figure is displayed twice. Here is the code (cell 1):</p>
  
  <pre><code>fig, ax = plt.subplots() 
  ax.plot([3,1,2,4,0,5,3,2,0,2,4])
  </code></pre>
  
  <p>... figure displayed ..., cell 2 (edit: thanks Thomas K for the improvement):</p>
  
  <pre><code>vline = ax.axvline(1)
  hline = ax.axhline(0.5)
  
  def set_cursor(x, y):
      vline.set_xdata((x, x))
      hline.set_ydata((y, y))
      display(fig)
  </code></pre>
  
  <p>and finally (cell 3):</p>
  
  <pre><code>interact(set_cursor, x=(1, 9, 0.01), y=(0, 5, 0.01))
  </code></pre>
  
  <p>shows again the figure with the widgets.</p>
  
  <p>So the question is:</p>
  
  <ol>
  <li>how can I inhibit the first figure display?</li>
  <li>is that the right way to do it or is there a better approach?</li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found an ipython config knob that, according to <a href="http://camillescott.github.io/blog/context-managers-for-ipython.html">this notebook</a>, allows inhibiting the figure display </p>
  
  <pre><code>%config InlineBackend.close_figures = False
  </code></pre>
  
  <p>While the example notebook works, I can't figure out how to use this option by itself (without the context manager class provided in the linked example) to hide a figure display.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I found <a href="http://ipython.org/ipython-doc/dev/interactive/qtconsole.html#inline-matplotlib">some documentation</a> of the <code>InlineBackend.close_figures</code> configurable.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Triggered by @shadanan answer, I want to clarify that my purpose is to add a cursor to an existing figure without redrawing the plot from scratch at each cursor movement. Merging the 3 cells in a single  cell:</p>
  
  <pre><code>fig, ax = plt.subplots()
  ax.plot([3,1,2,4,0,5,3,2,0,2,4])
  
  vline = ax.axvline(1)
  hline = ax.axhline(0.5)
  
  def set_cursor(x, y):
      vline.set_xdata((x, x))
      hline.set_ydata((y, y))
      display(fig)
  
  interact(set_cursor, x=(1, 9, 0.01), y=(0, 5, 0.01))
  </code></pre>
  
  <p>it "should" work but it doesn't. The first time the cell is executed it shows 2 figures. After widget interaction only 1 figure is displayed. This is the "strange behavior" that requires a workaround like the one shown in @shadanan answer. Can an ipython dev comment on this? Is it a bug? </p>