我试图在同一个情节上动画两条线。在四处寻找之后,我偶然发现了似乎让我走上正轨的this post。当我运行这段代码时,停滞的图形显示没有动画,给我一个错误AttributeError: 'AxesSubplot' object has no attribute 'set_data'
。我查了set_data
,它说它接受:2D阵列(行为x,y)或两个1D阵列。动画是否不工作,因为我将line1
和line2
指定给绘图而不是二维数组?我会很感激任何帮助,让这些线上我的情节动画,我已经尝试,并试图无济于事。谢谢!
fig, ax = plt.subplots(figsize=(16,8))
#Plot Lines
line1 = sns.lineplot('game_seconds_remaining', 'away_wp', data=game, color='#4F2683',linewidth=2)
line2 = sns.lineplot('game_seconds_remaining', 'home_wp', data=game, color='#869397',linewidth=2)
#Add Fill
ax.fill_between(game['game_seconds_remaining'], 0.5, game['away_wp'], where=game['away_wp']>.5, color = '#4F2683',alpha=0.3)
ax.fill_between(game['game_seconds_remaining'], 0.5, game['home_wp'], where=game['home_wp']>.5, color = '#869397',alpha=0.3)
#Plot Aesthetics - Can Ignore
plt.ylabel('Win Probability %', fontsize=16)
plt.xlabel('', fontsize=16)
plt.axvline(x=900, color='white', alpha=0.7)
plt.axvline(x=1800, color='white', alpha=0.7)
plt.axvline(x=2700, color='white', alpha=0.7)
plt.axhline(y=.50, color='white', alpha=0.7)
plt.suptitle('Minnesota Vikings @ Dallas Cowboys', fontsize=20, style='italic',weight='bold')
plt.title('Min 28, DAL 24 - Week 10 ', fontsize=16, style = 'italic',weight='semibold')
#Labels (And variable assignment for animation below)
x = ax.set_xticks(np.arange(0, 3601,900))
y1 = game['away_wp']
y2 = game['home_wp']
plt.gca().invert_xaxis()
x_ticks_labels = ['End','End Q3','Half','End Q1','Kickoff']
ax.set_xticklabels(x_ticks_labels, fontsize=12)
#Animation - Not working
def update(num, x, y1, y2, line1, line2):
line1.set_data(x[:num], y1[:num])
line2.set_data(x[:num], y2[:num])
return [line1,line2]
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y1, y2, line1, line2],
interval=295, blit=False)
1条答案
按热度按时间flvlnr441#
在
python 3.11.2
、pandas 2.0.0
、matplotlib 3.7.1
、seaborn 0.12.2
中测试看起来
sns
给出了AxesSublot
,你必须为这个Axes
得到line(s)
或者(因为两行都在同一个
Axes
上)编辑:
Google Colab
需要来源:Embedding Matplotlib Animations in Python (google colab notebook)
随机数据的最小工作代码
编辑:
没有
seaborn
,只有plt.plot()
。在开始时,我创建空行
line1, = plt.plot([], [])