散景日期范围滑块 [英] Bokeh DateRangeSlider

查看:27
本文介绍了散景日期范围滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在 Bokeh 工作,完成了一些关于 datacamp 的快速课程,但我一直坚持创建 DateRangeSlider.滑块本身对我来说看起来不错,更改滑块上的值可以,但我无法更改绘图.

I have recently started working in Bokeh, finished some quick course on datacamp and I got stuck on creating a DateRangeSlider. Slider itself looks ok to me, changing the values on the slider works, but I can't make it to change the plot.

不确定我缺少什么,因为我找不到有关使用此特定工具的适当教程/信息,因此非常感谢您提供任何帮助.

Not sure what am I missing as I can't find proper tutorials / info on using this particular tool, so any help on that would be highly appreciated.

代码如下:

data['periodFrom'] = pd.to_datetime(data['periodFrom']).dt.date
data = data.rename(columns={'MWh/h' : 'MWH'})
source = ColumnDataSource(data={
    'x'       : data.periodFrom,
    'y'       : data.MWH,
    'point'      : data.pointLabel,
    'direction'      : data.directionKey
})
xmin, xmax = min(data.periodFrom), max(data.periodFrom)
ymin, ymax = min(data['MWH']), max(data['MWH'])

plot = figure(title='Gas renomination', plot_height=400, plot_width=700,
              x_range=(xmin, xmax), y_range=(ymin, ymax))

plot.line(x='x', y='y', source=source)

plot.xaxis.axis_label ='Date'
plot.yaxis.axis_label = 'MWh/h'

def update_plot(attr, old, new):
    yr = slider.value
    new_data = {
    'x'       : data.loc[yr[0]:yr[1]].periodFrom,
    'y'       : data.loc[yr[0]:yr[1]].MWH,
    'point'      : data.loc[yr[0]:yr[1]].pointLabel,
    'direction'      : data.loc[yr[0]:yr[1]].directionKey
    }
    source.data = new_data

slider = DateRangeSlider(start=date(2016,1,1), end=date.today(), step=1, 
         value=(date(2016,1,1),date(2016,2,1)), title='Date', width=1000)

slider.on_change('value', update_plot)

curdoc().add_root(plot)
curdoc().add_root(slider)
show(plot)

这是我每次更改滑块值时在命令提示符中收到的错误消息:

And here's the error message I get in the commmand prompt every time I change slider's value:

2018-07-19 11:56:55,387 error handling message Message 'PATCH-DOC' (revision 1): KeyError(1451606400000,)

最好,皮奥特雷克

我在滑块上做了一些工作,现在没有出现任何错误,但是当我向任何方向移动滑块时,情节就会消失.单独过滤效果很好(即当我将 'data[data.index > yr[0]].periodFrom' 放在控制台中时),但使用新值更新绘图似乎有问题.

I did some work on the slider, now not raising any errors, but the plot vanishes when I move the slider in any direction. Individually filtering works well (i.e. when I put 'data[data.index > yr[0]].periodFrom' in the console), but it seems like there's something wrong with updating the plot with new values.

data['periodFrom'] = pd.to_datetime(data['periodFrom'])
data.index = data['periodFrom'].astype(np.int64) // 10 ** 9
data.index = data.index.astype(np.float)

data = data.rename(columns={'MWh/h' : 'MWH'})
source = ColumnDataSource(data={
    'x'       : data.periodFrom,
    'y'       : data.MWH,
    'point'      : data.pointLabel,
    'direction'      : data.directionKey
})

xmin, xmax = min(data.periodFrom), max(data.periodFrom)
ymin, ymax = min(data['MWH']), max(data['MWH'])

# Create the figure: plot
plot = figure(title='Gas renomination', plot_height=400, plot_width=700,
              x_range=(xmin, xmax), y_range=(ymin, ymax))

plot.line(x='x', y='y', source=source)

plot.xaxis.axis_label ='Date'
plot.yaxis.axis_label = 'MWh/h'

#slider
def update_plot(attr, old, new):
    yr = slider.value
    yr=list(yr)
    [float(i) for i in yr]
    new_data = {
        'x' : data[(data.index > yr[0]) & (data.index < yr[1])].periodFrom,
        'y' : data[(data.index > yr[0]) & (data.index < yr[1])].MWH,
        'point' : data[(data.index > yr[0]) & (data.index < yr[1])].pointLabel,
        'direction' : data[(data.index > yr[0]) & (data.index < yr[1])].directionKey}

print(new_data)
source.data = new_data

slider = DateRangeSlider(start=datetime(2016,1,2).timestamp(), 
end=datetime.today().timestamp(), step=1, value= 
(datetime(2016,2,2).timestamp(),datetime(2016,3,3).timestamp()), title='Date', 
width=1000)
slider.on_change('value', update_plot)

#dropdown

def update_dropdown(attr, old, new):
    point_select = select.value
    data_selected ={
        'x' : data[data['pointLabel'] == point_select].periodFrom,
        'y' : data[data['pointLabel'] == point_select].MWH,
        'point' : data[data['pointLabel'] == point_select].pointLabel,
        'direction' : data[data['pointLabel'] == point_select].directionKey}

    source.data=data_selected

select = Select(title="Point", options=pointlabels, value='Kondratki')
select.on_change('value', update_dropdown)

curdoc().add_root(select)
curdoc().add_root(plot)
curdoc().add_root(slider)

我的原始数据框是 40639 x 6,打印 new_data 后我得到一个字典:

My original dataframe is 40639 x 6, after printing new_data i get a dict:

我还在代码的第二部分添加了一个 Select 小部件,data_selected 的格式与 DateRangeSlider 中的格式相同(参见:图片).对我来说奇怪的是 Select 可以完美运行,而 DataRangeSlider 每次单击时都会使情节消失.

I also added a Select widget in the second part of the code, data_selected is the same format as in DateRangeSlider (see: image). What's strange for me is that Select works perfectly, while DataRangeSlider makes the plot vanish every time I click it.

推荐答案

虽然为方便起见可以使用 datetime 对象初始化滑块,但滑块值实际上表示为浮点时间戳,具体来说,是自 epoch 以来的毫秒数.这意味着您尝试直接使用该值索引您的数据框大致相当于:

Although sliders can be initialized with datetime objects for convenience, the slider value is actually represented as a floating point timestamp, specificically, the number of milliseconds since epoch. That means your attempts to index your dataframe directly with the value are roughly equivalent to:

data.loc[1451606400000].periodFrom

由于显而易见的原因,这不起作用.

Which does not work for obvious reasons.

根据名称 yr,您似乎想用年份值索引您的数据框?然后您需要将时间戳值转换为真实的日期时间和额外的年份:

Based on the name yr, it looks like you want to index your dataframe with values that are years? Then you need to convert the timestamp value to a real datetime and extra the year:

In [6]: from datetime import datetime

In [7]: datetime.fromtimestamp(1451606400000/1000).year
Out[7]: 2015

顺便说一句,没有理由在 Bokeh 服务器应用程序中使用或调用 show.

As an aside, there is never a reason to use or call show in a Bokeh server application.

这篇关于散景日期范围滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆