【问题标题】:How to get radio buttons to update histogram Matplotlib Python如何获取单选按钮来更新直方图 Matplotlib Python
【发布时间】:2020-07-28 06:54:27
【问题描述】:

在使用 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 Notebook 上运行。感谢您的帮助!
  • 您收到错误消息吗?或者它不是交互式的?确保在笔记本的开头包含%matplotlib widget(您需要安装ipympl)。
  • 当我使用我的课堂 jupyter 笔记本时,我没有收到错误,它没有显示图表或任何东西。我在我的桌面上尝试过,当我注释掉 %matplotlib 小部件时它工作了。
  • 您的代码是否显示了情节?我安装了python3.7.7matplotlib3.2.2,对我来说,它既适用于jupyter 笔记本,也适用于普通脚本。尝试将所有动画部分注释掉,然后绘制一个空图,然后逐步添加其他功能以查看它在哪里中断。

标签: python matplotlib animation histogram


【解决方案1】:

代码改编自this answer。 出于某种原因,您必须在图形更新之前单击两次,否则它应该可以正常工作。

# %matplotlib widget  # for jupyter notebook
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
from matplotlib.animation import FuncAnimation
import numpy as np

n_max = 200
x_curr = np.random.normal(-2.5, 1, size=n_max)
bins = np.arange(-4, 4, 0.5)

def animate(i):
    ax.clear()
    ax.set_title('Sampling the Normal Distribution')
    ax.set_ylabel('Frequency')
    ax.set_xlabel('Value')
    ax.annotate('n = {}'.format(i), [3, 25])
    ax.axis([-7, 5, 0, 30])
    ax.hist(x_curr[:i], bins=bins)


anis = []
def on_click(event):
    n = int(event[2:])

    for ani in anis:
        ani.event_source.stop()
        anis.remove(ani)
        del ani

    print(n)
    ani = FuncAnimation(fig, animate, frames=n, repeat=False)
    anis.append(ani)
    fig.canvas.draw_idle()

fig = plt.figure()
ax = fig.subplots()

rax = plt.axes([0.01, 0.2, 0.15, 0.25])
radio = RadioButtons(rax, ('n=20', 'n=50', 'n=100', 'n=200'))
radio.on_clicked(on_click)
animate(20)
plt.show()

【讨论】:

  • 非常感谢您花时间解决我的问题。我不确定我错过了什么,但是当我在 Jupyter 笔记本中运行它时,它只显示一个带有单选按钮的空白图。
猜你喜欢
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
相关资源
最近更新 更多