首页 > 解决方案 > 如何修复我的滑块?它没有按预期工作

问题描述

我正在尝试创建一个滑块来设置 matplotlib 中的 bin,这是我的代码:

%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib.pyplot import ion
import numpy as np
import matplotlib.animation as animation
from matplotlib.widgets import RadioButtons
from matplotlib.widgets import Slider


# generate 4 random variables from the random, gamma, exponential, and uniform distributions
sample_size = 10000
normal = np.random.normal(loc=0.0, scale=1.0, size=sample_size)
gamma = np.random.gamma(shape = 1.0, scale=1.0, size=sample_size)
uniform = np.random.uniform(low=0.0, high=10.0, size=sample_size)
exponential = np.random.exponential(scale=1.0, size=sample_size)

fig, sub_plt = plt.subplots()
plt.subplots_adjust(top=0.65) # Adjust subplot to not overlap with radio box

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.7, 0.25, 0.25], facecolor=axcolor)
axfreq = plt.axes([0.20, 0.02, 0.65, 0.03], facecolor=axcolor)
radio = RadioButtons(rax, ('Normal', 'Gamma', 'Uniform', 'Exponential'))
slide = Slider(axfreq, 'Bins', 10.0,200.0,valinit=30.0)

def dist_func(type_l):
    sub_plt.clear() # comment this line if you want to keep previous drawings
    dist_dict = {'Normal':normal, 'Gamma':gamma, 'Uniform':uniform, 'Exponential':exponential}
    data_type = dist_dict[type_l]
    sub_plt.hist(data_type, bins=100)


radio.on_clicked(dist_func)

def bin_func(val):
    slide_val = slide.val
    plt.figure()
    sub_plt.hist(data_type,bins=slide_val)
    fig.canvas.draw_idle()


 slide.on_changed(bin_func)

 plt.show()

我希望滑块的值来设置直方图的 bin。这会渲染滑块,但滑块不会按预期设置垃圾箱,实际上它没有做任何事情。有什么办法可以让垃圾箱按预期工作?

标签: pythonmatplotlib

解决方案


我认为sub_plt.hist(data_type,bins=slide_val)是问题所在, data_type 不是全局变量,因此您无法创建带有未定义变量的图。

我将画布重绘代码移到内部,dist_func以便单击其中一个单选按钮重绘绘图,而无需移动滑块。

确保滑块值是整数也很重要(必须有离散数量的 bin!)

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RadioButtons
from matplotlib.widgets import Slider

# generate 4 random variables from the random, gamma, exponential, and uniform distributions
sample_size = 10000
normal = np.random.normal(loc=0.0, scale=1.0, size=sample_size)
gamma = np.random.gamma(shape=1.0, scale=1.0, size=sample_size)
uniform = np.random.uniform(low=0.0, high=10.0, size=sample_size)
exponential = np.random.exponential(scale=1.0, size=sample_size)

fig, sub_plt = plt.subplots()
plt.subplots_adjust(top=0.65)  # Adjust subplot to not overlap with radio box

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.7, 0.25, 0.25], facecolor=axcolor)
axfreq = plt.axes([0.20, 0.02, 0.65, 0.03], facecolor=axcolor)
radio = RadioButtons(rax, ('Normal', 'Gamma', 'Uniform', 'Exponential'))
slide = Slider(axfreq, 'Bins', 10.0, 200.0, valinit=30.0, valstep=1)

dist_dict = {'Normal': normal, 'Gamma': gamma, 'Uniform': uniform, 'Exponential': exponential}


def dist_func(type_l, bins=100):
    sub_plt.clear()  # comment this line if you want to keep previous drawings
    data_type = dist_dict[type_l]
    sub_plt.hist(data_type, bins=bins)
    fig.canvas.draw_idle()


radio.on_clicked(dist_func)


def update(a):
    dist_func(radio.value_selected, bins=int(a))



# the final step is to specify that the slider needs to
# execute the above function when its value changes
slide.on_changed(update)
dist_func('Normal', bins=100)
plt.show()

推荐阅读