更新时间:2023-11-24 08:31:40
首先,我将张贴您的解决方案,然后一些说明:
First I will post you the solution, then some explanations:
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(0, 100))
N = 4
lines = [plt.plot([], [])[0] for _ in range(N)]
def init():
for line in lines:
line.set_data([], [])
return lines
def animate(i):
for j,line in enumerate(lines):
line.set_data([0, 2], [10 * j,i])
return lines
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
说明:
行= plt.plot([],[])
通过指定 plt.plot $返回列表的第一个元素C $ c>来的veriable 行
。
行= plt.plot([],[])
刚分配整个名单(只有一个元素的)。line, = plt.plot([], [])
assign the first element of the list returned by plt.plot
to the veriable line
.line = plt.plot([], [])
just assign the whole list (of only one element).替代行= [plt.plot([],[])[0] _范围内的(N)]
你可以这样做 =行plt.plot(*([[] []] * N))
只有一个绘图命令。我找到的第一个更具可读性,但品味的问题。
Alternative to lines = [plt.plot([], [])[0] for _ in range(N)]
you can do this lines = plt.plot( *([[], []]*N) )
with only one plot command. I found the first more readable but is matter of taste.