我制作了一個影片,其中藍條逐幀更新。問題是上一次迭代中的條形圖保留在畫布上而沒有被洗掉。如何使迭代繪圖不相加(非重疊幀,如在 gif 中更新從幀到幀的所有內容)?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.ticker import AutoMinorLocator, MaxNLocator
def count_elements(iterable):
val_dict = {}
val_list = list(iterable)
for i in set(val_list):
val_dict.update({i:val_list.count(i)})
return np.array(list(val_dict.keys())),np.array(list(val_dict.values()))
# Generate n rolls for a probability distribution of two spins of a wheel of fortune that has 4 equally large wedges. The wedges have numbers from one to 4 on them.
p = 0.25
prob_dist = np.array([[2,3,4,5,6,7,8],[p*p,2*p*p,3*p*p,4*p*p,3*p*p,2*p*p,p*p]])
mu = sum([prob_dist[0,i]*prob_dist[1,i] for i in range(prob_dist.shape[1])])
sig = sum([prob_dist[1,i]*(prob_dist[0,i]-mu)*(prob_dist[0,i]-mu) for i in range(prob_dist.shape[1])])
# true distribution
fig, ax = plt.subplots(figsize=(7,4))
ax.bar(prob_dist[0], prob_dist[1], width=0.5, align="center", ec=None, color='red', alpha=0.5)
# Experimental data
n = 10
rolls = np.random.randint(1,5,n) np.random.randint(1,5,n)
exp_vals, exp_count = count_elements(rolls)
ax.bar(exp_vals,exp_count/n, width=0.3, align="center", ec=None, color='blue', alpha=0.5)
def update(frame):
n = int(10 *1**frame)
rolls = np.random.randint(1,5,n) np.random.randint(1,5,n)
exp_vals, exp_count = count_elements(rolls)
ax.bar(exp_vals,exp_count/n, width=0.3, align="center", ec=None, color='blue', alpha=0.5)
ani = FuncAnimation(fig, update, frames=range(1,10),repeat=False,blit=False,interval=1000)
plt.show()
uj5u.com熱心網友回復:
似乎您只更新了條形的高度,所以您應該這樣做 - 更新條形的高度:
...
# true distribution
fig, ax = plt.subplots(figsize=(7,4))
ax.bar(prob_dist[0], prob_dist[1], width=0.5, align="center", ec=None, color='red', alpha=0.5)
#store the x-values
all_x_vals = prob_dist[0]
#catch the bar container for the update in the animation loop
bc = ax.bar(all_x_vals, 0, width=0.3, align="center", ec=None, color='blue', alpha=0.5)
def update(frame):
#not sure what this is as it will always be 10
n = int(10 *1**frame)
rolls = np.random.randint(1,5,n) np.random.randint(1,5,n)
exp_vals, exp_count = count_elements(rolls)
heights = np.zeros_like(all_x_vals)
heights[np.in1d(all_x_vals, exp_vals).nonzero()[0]] = exp_count/n
for b, h in zip(bc, heights):
b.set_height(h)
#sanity check, should be 1
#print(sum(b.get_height() for b in bc))
ani = FuncAnimation(fig, update, frames=range(0,10),repeat=False,blit=False,interval=1000)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449629.html
標籤:Python matplotlib 动画
