如何在不保留前一个条形图的情况下重新绘制条形图



我制作了一个动画,其中蓝色条逐帧更新。问题是,上一次迭代中的条形图会保留在画布上而不会被删除。我如何使迭代绘图不具有附加性(不重叠的帧,如在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()

似乎你只更新了条的高度,所以你应该准确地这样做-更新条的高度:

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

相关内容

最新更新