简体   繁体   English

我如何在 matplotlib 中获得一个动画图形?

[英]How do i get a graph to animate in matplotlib?

Hey i have been trying to increase the fourth bar on my bar graph by three everytime the animattion_frame function runs, but no matter what i do it does not want to work.嘿,我一直在尝试将条形图上的第四个条形增加三个,每次运行 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()

There are a lot of weird things going on with your code, but I tried to salvage most of it.你的代码有很多奇怪的地方,但我试图挽救大部分。 Is this what you are trying to achieve?这是你想要达到的目标吗?

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM