我正在尝试显示一个Python 2.7 Matplotlib图,动画显示2行,循环浏览11个文件,每个文件包含不同的月份数据
行数据保存在名为CoinMarketData [i]的数据帧中,用于第1帧到第11帧,列为“Log MC”和“Log EMC”。
到目前为止的代码:
fig = plt.figure()
ax = plt.axes(xlim=(0,100), ylim=(0,30))
N=11
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):
# I think i need to put lists of the X and Y data in here
lines.set_data(x, y) # set_data only takes 2 arguements...how do i set both y and y2 to the lines?
return lines
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
答案 0 :(得分:1)
如果您想要2行,请不要创建其中的11行。由于每行属于一列,因此可以单独设置数据。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
import numpy as np
N=11
dataframes = [pd.DataFrame({"x":np.sort(np.random.rand(10)*100),
"y1":np.random.rand(10)*30,
"y2":np.random.rand(10)*30}) for _ in range(N)]
fig = plt.figure()
ax = plt.axes(xlim=(0,100), ylim=(0,30))
lines = [plt.plot([], [])[0] for _ in range(2)]
def animate(i):
lines[0].set_data(dataframes[i]["x"], dataframes[i]["y1"])
lines[1].set_data(dataframes[i]["x"], dataframes[i]["y2"])
return lines
anim = animation.FuncAnimation(fig, animate,
frames=N, interval=20, blit=True)
plt.show()