交互式更新直方图

时间:2020-01-01 19:20:36

标签: matplotlib interactive

我想使用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()一起使用。

0 个答案:

没有答案
相关问题