我正在寻找一个动画/不断增长的条形图。该图基本上包含6个矩形条,每个条都有一个特定的值。
我面临的问题是该图在Y轴上一直增长到最大值,而应该在条形图的对应值处停止。
我尝试过的代码使条形动画达到Y轴上的最大值。 Growing matplotlib bar charts
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
%matplotlib notebook
fig = plt.figure()
position = np.arange(6) + .5
plt.tick_params(axis = 'x', colors = '#072b57')
plt.tick_params(axis = 'y', colors = '#072b57')
speeds = [.01, .02, .03, .04, .01, .02]
heights = [0, 0, 0, 0, 0, 0]
# Bar plot should animate up to these values
# Adding this portion is making the plot static
#heights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
rects = plt.bar(position, heights, align = 'center', color=['red', 'orange', 'blue', 'pink', 'green','purple'])
plt.xticks(position, ('Anger', 'Sadness', 'Disgust', 'Fear', 'Happy', 'Surprise'))
plt.xlabel('Emotion', color = '#072b57')
plt.ylabel('Probabilities', color = '#072b57')
plt.title('Emotion - Ally', color = '#072b57')
plt.ylim((0,1))
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==1, heights)):
heights = [0, 0, 0, 0, 0, 0]
else:
heights = [min(h+s,1) for h,s in zip(heights,speeds)]
# Bar plot should animate up to these values
# Adding this portion is making the plot static
#heights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
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()
答案 0 :(得分:0)
您需要更多的计算,例如
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 = [.01, .02, .03, .04, .01, .02]
heights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
rects = plt.bar(position, np.zeros_like(heights), align = 'center',
color=['red', 'orange', 'blue', 'pink', 'green','purple'])
plt.xticks(position, ('Anger', 'Sadness', 'Disgust', 'Fear', 'Happy', 'Surprise'))
plt.xlabel('Emotion', color = '#072b57')
plt.ylabel('Probabilities', color = '#072b57')
plt.title('Emotion - Ally', color = '#072b57')
plt.ylim((0,1))
plt.xlim((0,6))
plt.grid(True)
frames = 200
min_speed = np.min(speeds)
def init():
return rects
def animate(i):
for h,r,s in zip(heights,rects, speeds):
new_height = i / (frames-1) * h * s / min_speed
new_height= min(new_height, h)
r.set_height(new_height)
return rects
anim = animation.FuncAnimation(fig, animate, init_func=init,frames=frames, interval=20, blit=True, repeat=False)
plt.show()
答案 1 :(得分:0)
现在,您的柱高从min(h+s,1)
派生,这意味着它将以一定的速度(或步长)增长,直到达到1
的值。
如果要限制高度,则应创建一个各自的数组,例如max_heights = [0.5, .6, 1.0, 0.6, 0.1, 1.0]
,并将else
情况下的高度计算更改为heights = [min(h+s,mh) for h,s,mh in zip(heights,speeds,max_heights)]
。
总结:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
# %matplotlib notebook
fig = plt.figure()
position = np.arange(6) + .5
plt.tick_params(axis = 'x', colors = '#072b57')
plt.tick_params(axis = 'y', colors = '#072b57')
speeds = [.01, .02, .03, .04, .01, .02]
heights = [0, 0, 0, 0, 0, 0]
# Bar plot should animate up to these values
# Adding this portion is making the plot static
max_heights = [0.5, .6, 1.0, 1.6, 0.1, 1.0]
rects = plt.bar(position, heights, align = 'center', color=['red', 'orange', 'blue', 'pink', 'green','purple'])
plt.xticks(position, ('Anger', 'Sadness', 'Disgust', 'Fear', 'Happy', 'Surprise'))
plt.xlabel('Emotion', color = '#072b57')
plt.ylabel('Probabilities', color = '#072b57')
plt.title('Emotion - Ally', color = '#072b57')
plt.ylim((0,1))
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==1, heights)):
heights = [0, 0, 0, 0, 0, 0]
else:
heights = [min(h+s,mh) for h,s,mh in zip(heights,speeds,max_heights)]
# Bar plot should animate up to these values
# Adding this portion is making the plot static
#heights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
print heights
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()