多行python动画
我试图显示一个Python2.7MatPlotLib图形,动画显示2行,循环遍历11个文件,每个文件包含不同的月份数据 线路数据保存在称为CoinMarketData[i]的数据帧中,用于帧1到11,在“Log MC”和“Log EMC”列中 到目前为止,我掌握的代码是:多行python动画,python,matplotlib,Python,Matplotlib,我试图显示一个Python2.7MatPlotLib图形,动画显示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)]
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()
如果想要有两行,不要创建其中的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()
如果想要有两行,不要创建其中的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()