首页 > 解决方案 > 如何在 matplotlib 中获得动画效果?

问题描述

嘿,每次运行 animattion_frame 函数时,我一直试图将条形图上的第四条增加三,但无论我做什么,它都不想工作。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


N = 5
menMeans = [20, 35, 30, 35, 27]



ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

fig, ax1 = plt.subplots()

#plt.bar(ind, menMeans, width)


ax=plt.yticks(np.arange(0, 81, 10))



def animation_frame(ind,i,menMeans):
    #x_data.append(i * 10)
    menMeans[3]=menMeans[3]+3
    ax=plt.clear()
    ax=plt.bar(ind,menMeans,width)
    return ax

animation = FuncAnimation(fig,fargs=(menMeans,ind), func=animation_frame, interval=100)
plt.show()

标签: pythonmatplotlib

解决方案


您的代码发生了很多奇怪的事情,但我试图挽救其中的大部分。这是你想要达到的目标吗?

N = 5
menMeans = [20, 35, 30, 35, 27]
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

fig, ax1 = plt.subplots()
ax1.set_yticks(np.arange(0, 81, 10))

def animation_frame(i, menMeans, ind):
    menMeans[3] += 3
    ax1.cla()
    ret = ax1.bar(ind,menMeans,width)
    return ret,

animation = FuncAnimation(fig,fargs=(menMeans, ind), func=animation_frame, interval=100)
plt.show()

在此处输入图像描述


推荐阅读