Python 3.x Python:Matplotlib:以秒为单位按时间更新图形

Python 3.x Python:Matplotlib:以秒为单位按时间更新图形,python-3.x,time,matplotlib,Python 3.x,Time,Matplotlib,我有一个系列要在y轴上绘制 y = [3,4,5,1,4,7,4,7,1,9] 然而,我想以秒为单位绘制它。我是这样做的 import time def xtime(): t = time.strftime("%H%M%S") t = int(t) xtime = [t] while xtime: t = time.strftime("%H%M%S") t = int(t) xtime.extend([t])

我有一个系列要在y轴上绘制

y = [3,4,5,1,4,7,4,7,1,9]
然而,我想以秒为单位绘制它。我是这样做的

import time
def xtime():

  t = time.strftime("%H%M%S")
  t = int(t)
  xtime = [t]

  while xtime:

        t = time.strftime("%H%M%S")
        t = int(t)
        xtime.extend([t])
        time.sleep(1)
当我想以每秒的速度绘制y上的每一个数字时,我遇到了问题。请在这里更正我的代码

import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
def animate(i):

    x = xtime()
    y = [3,4,5,1,4,7,4,7,1,9]
    plt.plot(x,y)

ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
xtime函数最初称为代码

谢谢

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# Y data
ydata = [3,4,5,1,4,7,4,7,1,9]
# how many points
N = len(ydata)
# make x data
xdata = np.arange(N)

def animate(i):
    # update the date in our Line2D artist
    # note that when run this will look at the global namespace for
    # an object called `ln` which we will define later
    ln.set_data(xdata[:i], ydata[:i])
    # return the updated artist for the blitting
    return ln,

# make our figure and axes    
fig, ax = plt.subplots()
# make the artist we will be using.  Note this was used in `animate`
ln,  = ax.plot([], [], animated=True)
# set the axes limits
ax.set_xlim(0, N)
ax.set_ylim(0, 10)

# run the animation.  Keeping a ref to the animation object is important
# as if it gets garbage collected it takes you timer and callbacks with it
ani = animation.FuncAnimation(fig, animate, frames=N, interval=1000, blit=True)