I'm trying to create an animated plot using matplotlib
. It works as expected when i'm using integers for the X
values:
#!/usr/bin/env python
import os
import random
import numpy as np
from datetime import datetime as dt, timedelta
from collections import deque
import matplotlib.pyplot as plt # $ pip install matplotlib
import matplotlib.animation as animation
%matplotlib notebook
npoints = 30
x = deque([0], maxlen=npoints)
y = deque([0], maxlen=npoints)
fig, ax = plt.subplots()
[line] = ax.plot(x, y)
def get_data():
t = random.randint(-100, 100)
return t * np.sin(t**2)
def data_gen():
while True:
yield get_data()
def update(dy):
x.append(x[-1] + 1)
y.append(dy)
line.set_data(x, y)
ax.relim()
ax.autoscale_view(True, True, True)
return line, ax
plt.rcParams['animation.convert_path'] = 'c:/bin/convert.exe'
ani = animation.FuncAnimation(fig, update, data_gen, interval=500, blit=True)
#ani.save(os.path.join('C:/','temp','test.gif'), writer='imagemagick', fps=30)
plt.show()
this produces the following animation:
however as soon as i'm trying to use datetime
values as x
values - the plot is empty:
npoints = 30
x = deque([dt.now()], maxlen=npoints) # NOTE: `dt.now()`
y = deque([0], maxlen=npoints)
fig, ax = plt.subplots()
[line] = ax.plot(x, y)
def get_data():
t = random.randint(-100, 100)
return t * np.sin(t**2)
def data_gen():
while True:
yield get_data()
def update(dy):
x.append(dt.now()) # NOTE: `dt.now()`
y.append(dy)
line.set_data(x, y)
ax.relim()
ax.autoscale_view(True, True, True)
return line, ax
plt.rcParams['animation.convert_path'] = 'c:/bin/convert.exe'
ani = animation.FuncAnimation(fig, update, data_gen, interval=1000, blit=True)
#ani.save(os.path.join('C:/','temp','test.gif'), writer='imagemagick', fps=30)
plt.show()
what am I doing wrong?
PS I'm using matplotlib
version: 2.1.2