我正在使用 Panel 的滑块小部件来检查一些数据,并且我想将此小部件的值用作其他函数的参数。我基于这个构建了自己的代码:
import hvplot.pandas
import panel as pn
import holoviews as hv
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
pn.extension()
bins = pn.widgets.IntSlider(name='Number of bins', start=5, end=25, step=5, value=10)
kde = pn.widgets.Checkbox(name='Show density estimate')
observations = pn.widgets.Checkbox(name='Show individual observations')
bandwidth = pn.widgets.FloatSlider(name='KDE Bandwidth', start=0.1, end=1)
@pn.depends(bins.param.value, kde.param.value,
observations.param.value, bandwidth.param.value)
def get_plot(bins, kde, obs, bw):
plot = sea_surface_temperature.hvplot.hist(bins=bins, normed=True, xlabel='Temperature (C)', padding=0.1)
if kde:
plot *= sea_surface_temperature.hvplot.kde().opts(bandwidth=bw)
if obs:
plot *= hv.Spikes(sea_surface_temperature, 'temperature').opts(
line_width=0.1, alpha=0.1, spike_length=-0.01)
return plot
widgets = pn.WidgetBox('## Sea surface temperatures', bins, observations, kde)
def add_bandwidth(event):
if event.new:
widgets.append(bandwidth)
else:
widgets.remove(bandwidth)
kde.param.watch(add_bandwidth, 'value')
pn.Row(widgets, get_plot).servable()
将我的问题放在前面代码的上下文中,我想使用 bins 的值。为了做到这一点,我将最终输出更改为:
output = (pn.Row(widgets, get_plot).servable(), bins)
Doingoutput[1]
显示绘图,但output[2]
显示滑块小部件而不是所选整数。
如果我将 get_plot 函数的输出更改为return (plot,bins)
最终图,则显示小部件加上与 doprint(plot)
而不是直方图相同的内容。
这是我用来开发我的代码的来源: https ://panel.pyviz.org/gallery/simple/temperature_distribution.html#gallery-temperature-distribution