Skip to content Skip to sidebar Skip to footer

Dynamically Update Plot In Ipython Notebook

As referred in this question, I am trying to update a plot dynamically in an iPython notebook (in one cell). The difference is that I don't want to plot new lines, but that my x_da

Solution 1:

Currently, you are creating a new Axes object for every time you plt.plot in the loop.

So, if you clear the current axis (plt.gca().cla()) before you use plt.plot, and put the legend inside the loop, it works without the legend growing each time:

import numpy as np
import time
from IPython import display

x = []
y = []
for i inrange(10):
    x = np.append(x, i)
    y = np.append(y, i**2)
    plt.gca().cla() 
    plt.plot(x,y,label='test')
    plt.legend()
    display.clear_output(wait=True)
    display.display(plt.gcf()) 
    time.sleep(0.5) 

EDIT: As @tcaswell pointed out in comments, using the %matplotlib notebook magic command gives you a live figure which can update and redraw.

Post a Comment for "Dynamically Update Plot In Ipython Notebook"