Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plots that varies over the time on Python Matplotlib with Jupyter

In the following lines I report a code that generates a plot changing over the time with Python on Anaconda Spyder

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
    y = np.sin(np.pi*x*N)
    line, = ax.plot(x, y)
    plt.draw()
    plt.pause(0.5)
    line.remove()

I would like to do the some with Jupyter, but it is not possible. Particularly it seems that the Matplotlib method .pause() does not exist on Jupyter. Is there anyone who can explain me this difference and can help me building up a code for plots variating over the time with Python on Jupyter, please?

like image 856
Stefano Fedele Avatar asked Dec 29 '25 20:12

Stefano Fedele


1 Answers

It works for me if I select an interactive backend using the magic command %matplotlib; it is likely that your Jupyter notebook settings are set to display plots inline.

import matplotlib.pyplot as plt
import numpy as np

%matplotlib

x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
    y = np.sin(np.pi*x*N)
    line, = ax.plot(x, y)
    plt.draw()
    plt.pause(0.5)
    line.remove()

To restore your setings, use the magic %matplotlib inline

like image 78
Reblochon Masque Avatar answered Dec 31 '25 12:12

Reblochon Masque