Я пытаюсь построить анимацию в реальном времени, используя python matplotlib animation. Фрагмент может выполняться, но с ошибкой, которую я не смог идентифицировать, появляется сообщение об ошибке «ValueError: Невозможно преобразовать -1 в дату. Это часто происходит, если значения, отличные от datetime, передаются на ось, которая ожидает объекты datetime. ' Но он нигде не показывает номер строки. Спасибо за любую помощь.
import random
import time
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
from matplotlib import animation
import datetime
# Plot parameters
fig, ax = plt.subplots()
line, = ax.plot([], [], 'k-', label = 'ABNA: Price', color = 'blue')
legend = ax.legend(loc='upper right',frameon=False)
plt.setp(legend.get_texts(), color='grey')
ax.margins(0.05)
ax.grid(True, which='both', color = 'grey')
# Creating data variables
x = [datetime.datetime.now()]
y = [1]
def init():
line.set_data(x[:1],y[:1])
line.axes.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
return line,
def animate(args):
# Args are the incoming value that are animated
animate.counter += 1
i = animate.counter
win = 60
imin = max(0, i - win)
x.append(args[0])
y.append(args[1])
xdata = x[imin:i]
ydata = y[imin:i]
line.set_data(xdata, ydata)
line.set_color("red")
plt.title('ABNA CALCULATIONS', color = 'grey')
plt.ylabel("Price", color ='grey')
plt.xlabel("Time", color = 'grey')
ax.set_facecolor('black')
ax.xaxis.label.set_color('grey')
ax.tick_params(axis='x', colors='grey')
ax.yaxis.label.set_color('grey')
ax.tick_params(axis='y', colors='grey')
ax.relim()
ax.autoscale()
return line,
animate.counter = 0
def frames1():
# Generating time variable
while True:
x = datetime.datetime.now()
y = random.randint(250,450)/10
yield (x,y)
anim = animation.FuncAnimation(fig, animate,init_func=init,frames=frames1)
plt.show()
ValueError: Невозможно преобразовать -1 в дату. Это часто происходит, если значения, отличные от datetime, передаются на ось, которая ожидает объекты datetime.