我想使用matplotlib
交互式地更新直方图。第一帧画得很好,我可以单击单选按钮,但是什么也没发生。
MWE:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
import matplotlib.animation as animation
from matplotlib.widgets import Slider, Button, RadioButtons
#Generate distributions
x1 = np.random.normal(0, 3, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
#Set up plot area
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.3, bottom=0.25)
bins = np.arange(-9, 9, 0.5)
l = plt.hist(x1,bins=bins,density=True)
#Radio buttons
rax = plt.axes([0.0, 0.5, 0.2, 0.15], facecolor="white")
radio = RadioButtons(rax, ('Normal', 'Gamma'), active=0)
#Update plot
def distfunc(label):
funcdict = {'Normal' : x1, 'Gamma': x2}
data = funcdict[label]
l.set_data(data) #This is probably the problem
plt.draw()
radio.on_clicked(distfunc)
plt.show
我的猜测是我尝试使用的set_data
方法存在问题,但是我无法弄清楚如何更新直方图的数据。我还尝试了set_xdata()
和set_ydata()
。例如,我知道我可以将set_ydata()
与plt.plot()
一起使用。