Python 字符串动画错误-matplotlib
我正在尝试在Python 字符串动画错误-matplotlib,python,pandas,matplotlib,animation,Python,Pandas,Matplotlib,Animation,我正在尝试在matplotlib中制作动画。我以前使用过这个code,但现在返回一个错误。我不确定更新是否导致了这种情况 下面是代码。这是以前的工作。但现在它返回一个错误: raise TypeError("invalid type comparison") TypeError: invalid type comparison 例如: import matplotlib.pyplot as plt import matplotlib.animation as animation im
matplotlib
中制作动画。我以前使用过这个code
,但现在返回一个错误。我不确定更新是否导致了这种情况
下面是代码
。这是以前的工作。但现在它返回一个错误:
raise TypeError("invalid type comparison")
TypeError: invalid type comparison
例如:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import pandas as pd
d = ({
'Time' : [1,2,3,4,5,6,7,8,9,10],
})
df = pd.DataFrame(data = d)
fig, ax = plt.subplots(figsize = (10,6))
#Event Table
Events_table = plt.table(cellText= [[''],[''],[''],[''],['']],
colWidths = [1],
rowLabels=['Time','1','2','3','4'],
colLabels=['Events'],
bbox = [0.124, 0.75, 0.236, 0.22])
Frame_number = df['Time']
label = plt.text(-180, 50, Frame_number, fontsize = 8, ha = 'center')
def animate(i) :
label.set_text(Frame_number[i+1])
ani = animation.FuncAnimation(fig, animate, np.arange(0,10),# init_func = init,
interval = 100, blit = False)
plt.draw()
我怀疑这段代码在以前的版本中是否有效;在任何情况下都存在以下问题:
- 我想您应该在动画开始之前将标签设置为系列的第一个元素
label = plt.text(0, 0, Frame_number[0])
- 如果使用
i+1
为序列编制索引,动画需要在最后一个索引之前停止一个索引
ani = animation.FuncAnimation(..., frames=np.arange(0, len(Frame_number)-1) )
完整代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import pandas as pd
df = pd.DataFrame(data = { 'Time' : [1,2,3,4,5,6,7,8,9,10], })
fig, ax = plt.subplots(figsize = (10,6))
Frame_number = df['Time']
label = plt.text(0, 0, Frame_number[0])
def animate(i) :
label.set_text(Frame_number[i+1])
ani = animation.FuncAnimation(fig, animate, np.arange(0,len(Frame_number)-1),
interval = 100, blit = False)
plt.show()
试试label=plt.text(-180,50,str(Frame\u number[0]),fontsize=8,ha='center')
和label.set\u text(str(Frame\u number[i+1])
。谢谢@Thomas Kuhn