Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取单选按钮以更新直方图Matplotlib Python_Python_Matplotlib_Animation_Histogram - Fatal编程技术网

如何获取单选按钮以更新直方图Matplotlib Python

如何获取单选按钮以更新直方图Matplotlib Python,python,matplotlib,animation,histogram,Python,Matplotlib,Animation,Histogram,在使用matplotlib.animation时,我很难获得更新直方图的单选按钮 我想展示正态分布的随机抽样。它以n=20开始。动画适用于n=20。单选按钮还添加了几个选项:n=50、n=100、n=200 下面是我的代码。谢谢你的帮助 %matplotlib notebook import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np n = 20 x = np.ran

在使用matplotlib.animation时,我很难获得更新直方图的单选按钮

我想展示正态分布的随机抽样。它以n=20开始。动画适用于n=20。单选按钮还添加了几个选项:n=50、n=100、n=200

下面是我的代码。谢谢你的帮助

%matplotlib notebook
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np

n = 20
x = np.random.normal(-2.5, 1,size=n)

# create the function that will do the plotting, where curr is the current frame
def update(curr):
    # check if animation is at the last frame, and if so, stop the animation a
    if curr == n: 
        a.event_source.stop()
    plt.cla()
    bins = np.arange(-4, 4, 0.5)
    plt.hist(x[:curr], bins=bins)
    plt.axis([-7,5,0,30])
    plt.gca().set_title('Sampling the Normal Distribution')
    plt.gca().set_ylabel('Frequency')
    plt.gca().set_xlabel('Value')
    plt.annotate('n = {}'.format(curr), [3,25])   

#Plot    
fig = plt.figure()

#Create radio button
rax = plt.axes([0.01, 0.2, 0.15, 0.15])
radio = RadioButtons(rax, ('n=50', 'n=100', 'n=200'))

def samplesize(label):
    if label == 50:
        n=50
        a=animation.FuncAnimation(fig, update, interval=100)  
    elif label == 100:
        n=100
        a=animation.FuncAnimation(fig, update, interval=100)  
    else:        
        n=200
        a=animation.FuncAnimation(fig, update, interval=100)  
    plt.draw()  


radio.on_clicked(samplesize)    
a = animation.FuncAnimation(fig, update, interval=100)  
aax = plt.axes([0.3, 0.1, 0.6, 0.6])   
plt.show()
该代码改编自。 由于某些原因,在图形更新之前,您必须始终单击两次,否则它应该可以正常工作

jupyter笔记本电脑的
#%matplotlib小部件
将matplotlib.pyplot作为plt导入
从matplotlib.widgets导入单选按钮
从matplotlib.animation导入FuncAnimation
将numpy作为np导入
n_max=200
x_curr=np.随机.正常(-2.5,1,大小=n_最大值)
垃圾箱=np.arange(-4,4,0.5)
定义动画(i):
ax.clear()
ax.set_title(‘正态分布抽样’)
ax.set_ylabel('频率')
ax.set_xlabel('值')
注释('n={}'。格式(i),[3,25])
ax.轴([-7,5,0,30])
ax.hist(x_curr[:i],存储箱=存储箱)
anis=[]
单击时的def(事件):
n=int(事件[2:])
对于ani,在anis中:
ani.event_source.stop()
anis.移除(ani)
德拉尼
打印(n)
ani=FuncAnimation(图,动画,帧=n,重复=False)
anis.append(ani)
图canvas.draw_idle()
图=plt.图()
ax=图子批次()
rax=plt.轴([0.01,0.2,0.15,0.25])
单选按钮=单选按钮(rax,('n=20','n=50','n=100','n=200'))
收音机。单击时(单击时)
制作动画(20)
plt.show()

非常感谢您抽出时间回答我的问题。我不确定我错过了什么,但当我在Jupyter笔记本上运行它时,它只是显示了一个带有单选按钮的空白绘图。我的答案对你有用吗?我无法让它在Jupyter笔记本上工作。谢谢你的帮助!你收到错误信息了吗?或者它不是互动的?确保在笔记本的开头包含
%matplotlib小部件
(您需要安装
ipympl
)。当我使用我的类jupyter笔记本时,我没有发现错误,它没有显示图形或任何东西。我在桌面上试用过,当我注释掉%matplotlib小部件时,它起作用了。您的代码是否显示了一个绘图?我安装了
python
3.7.7
matplotlib
3.2.2
,对我来说,它既可以与jupyter笔记本电脑一起使用,也可以作为普通脚本使用。尝试注释掉所有动画部分,只绘制一个空图形,然后一步一步添加其他功能,看看它在哪里中断。