python - 如何修复我的滑块?它没有按预期工作
问题描述
我正在尝试创建一个滑块来设置 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。这会渲染滑块,但滑块不会按预期设置垃圾箱,实际上它没有做任何事情。有什么办法可以让垃圾箱按预期工作?
解决方案
我认为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()
推荐阅读
- apache-nifi - Nifi 将 Azure Blob account_name/account_key 存储在一个地方
- sql - 展平重复行 - Oracle SQL
- excel - 不使用选择自动填充
- php - 为什么某些音频文件无法在 Twilio Play 中播放?
- ssl - 客户端无法通过 SSL 连接到 Apache Artemis
- class - 返回 null 的类列表中的字符串
- c# - VS2015 中的所有代码文件都被检出
- reactjs - 根据道具值显示组件反应
- google-cloud-platform - 无法开始在 Web UI 上训练我的 GCP AutoML 实体提取模型
- asp.net-core - Blazor 参数是否会带来额外的开销?