我正在尝试从下面的代码创建一个动态直方图。我可以为每个时间创建单独的直方图,但是我无法通过matplotlib.animation
函数或从模拟matplotlib教程中的代码来实现动画效果。
import numpy as np
import matplotlib.pyplot as plt
betas = [] # some very long list
entropy = [] # some very long list
for time in [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0 , 3.5, 4.0, 4.5 5.0, 5.5, 6.0, 6.5 , 7.0, 7.5, 8.0 , 8,5 , 9.0, 9.5 , 10.0]:
plt.figure('entropy distribution at time %s ' % time)
indexbetas = {i for i, j in enumerate(betas) if j == time}
desiredentropies = [x for i, x in enumerate(entropy) if i in indexbetas] #the desiredentropies list depends on time
n, bins, patches = plt.hist(desiredentropies, 20, alpha=0.75 , label = 'desired entropies')
plt.xlabel(r"$S_{(\time=%d)}$" % time, fontsize=20)
plt.ylabel('Frequency of entropies')
plt.legend()
plt.grid(True)
plt.show()
我在特定的问题上遇到了困难,那就是如何填充我的 desiredentropies
列表,这个列表依赖于动画中 time
列表中的元素。
animation.FuncAnimation
。 - furasanimation.FuncAnimation
作为for
的替代品。你必须使用它来替换for
。animation.FuncAnimation
会多次调用你的代码,并传入不同的值(即时间),每次调用后都会有一小段延迟,以便你可以看到它的动画效果。 - furas