I used the exact code from this StackOverflow question.
When I run this code, this is all I see:
I wish I had more details to provide but I'm confused as to why even this simple example is not showing up correctly.
I used the exact code from this StackOverflow question.
When I run this code, this is all I see:
I wish I had more details to provide but I'm confused as to why even this simple example is not showing up correctly.
As I see you use Jupyter Notebook, so you should run this command to to show up matplotlib
results:
%matplotlib inline
also you need there to use HTML
from IPython.display
. Then pass your animation converted to html video to this HTML
function:
HTML(anim.to_html5_video())
There is a full example below. I'm using the code you cited in your question and changed it little bit:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
dt = 0.01
tfinal = 5.0
x0 = 0
sqrtdt = np.sqrt(dt)
n = int(tfinal/dt)
xtraj = np.zeros(n+1, float)
trange = np.linspace(start=0,stop=tfinal ,num=n+1)
xtraj[0] = x0
for i in range(n):
xtraj[i+1] = xtraj[i] + np.random.normal()
x = trange
y = xtraj
# animation line plot example
fig = plt.figure(4)
ax = plt.axes(xlim=(-5, 5), ylim=(0, 5))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
line.set_data(x[:i], y[:i])
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1,interval=200, blit=False)
HTML(anim.to_html5_video())