gpt4 book ai didi

python - 不断增长的 matplotlib 条形图

转载 作者:行者123 更新时间:2023-11-30 23:29:08 25 4
gpt4 key购买 nike

我在 matplotlib 中有一个像这样的条形图:

import numpy as np
import matplotlib.pyplot as plt

position = np.arange(6) + .5

plt.tick_params(axis = 'x', colors = '#072b57')
plt.tick_params(axis = 'y', colors = '#072b57')

rects = plt.bar(position, (0, 0, 0, 0, 0, 0), align = 'center', color = '#b8ff5c')
plt.xticks(position, ('A', 'B', 'C', 'D', 'E', 'F'))

plt.xlabel('X Axis', color = '#072b57')
plt.ylabel('Y Axis', color = '#072b57')
plt.title('My Chart', color = '#072b57')

plt.grid(True)
plt.show()

现在所有图表级别都为 0,但我想让图表中的每一列以不同的速度从 0 开始增长,直到它们都达到最大值,例如 100。例如,在运行应用程序的过程中,图表将如下所示: enter image description here

剩余的列仍然增长,直到它们都达到最大值,然后程序结束。

现在我想问matplotlib中有什么东西可以完成这项工作吗?

最佳答案

是的,你可以在matplotlib中做动画,你需要使用matplotlib.animationthis blog进行介绍。接下来你怎么能做你想做的事:

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


fig = plt.figure()

position = np.arange(6) + .5

plt.tick_params(axis = 'x', colors = '#072b57')
plt.tick_params(axis = 'y', colors = '#072b57')

speeds = [1, 2, 3, 4, 1, 2]
heights = [0, 0, 0, 0, 0, 0]
rects = plt.bar(position, heights, align = 'center', color = '#b8ff5c')
plt.xticks(position, ('A', 'B', 'C', 'D', 'E', 'F'))

plt.xlabel('X Axis', color = '#072b57')
plt.ylabel('Y Axis', color = '#072b57')
plt.title('My Chart', color = '#072b57')

plt.ylim((0,100))
plt.xlim((0,6))

plt.grid(True)

rs = [r for r in rects]

def init():
return rs

def animate(i):
global rs, heights
if all(map(lambda x: x==100, heights)):
heights = [0, 0, 0, 0, 0, 0]
else:
heights = [min(h+s,100) for h,s in zip(heights,speeds)]
for h,r in zip(heights,rs):
r.set_height(h)
return rs

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

plt.show()

关于python - 不断增长的 matplotlib 条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com