我想创建一个交互式图表,可以在 X 轴上绘制日期,Y 轴上绘制值 Y。目前来看还不错。现在我想通过 DateRangeSlider 调整 x 轴的限制 xmin 和 xmax,但是我不明白 js 回调函数(最终我想获得一个独立的html文件),而且由于我不知道如何从函数内部打印值且不产生任何错误,所以现在我不知道该怎么做。
以下是代码的运行示例:
以下是代码的运行示例:
import numpy as np
import pandas as pd
from datetime import datetime
from bokeh.models import ColumnDataSource, DatetimeTickFormatter, HoverTool
from bokeh.models.widgets import DateRangeSlider
from bokeh.layouts import layout, column
from bokeh.models.callbacks import CustomJS
from bokeh.plotting import figure, output_file, show, save
datesX = pd.date_range(start='1/1/2018', periods=100)
valuesY = pd.DataFrame(np.random.randint(0,25,size=(100, 1)), columns=list('A'))
source = ColumnDataSource(data={'x': datesX, 'y': valuesY['A']})
# output to static HTML file
output_file('file.html')
hover = HoverTool(tooltips=[('Timestamp', '@x{%Y-%m-%d %H:%M:%S}'), ('Value', '@y')],
formatters={'x': 'datetime'},)
date_range_slider = DateRangeSlider(title="Zeitrahmen", start=datesX[0], end=datesX[99], \
value=(datesX[0], datesX[99]), step=1, width=300)
# create a new plot with a title and axis labels
p = figure(title='file1', x_axis_label='Date', y_axis_label='yValue', x_axis_type='datetime',
tools="pan, wheel_zoom, box_zoom, reset", plot_width=300, plot_height=200)
# add a line renderer with legend and line thickness
p.line(x='x', y='y', source=source, line_width=2)
p.add_tools(hover)
callback = CustomJS(args=dict(source=source), code="""
##### what to do???
source.change.emit();
""")
date_range_slider.js_on_change('value', callback)
layout = column(p, date_range_slider)
# show the results
show(layout)
我尝试根据stackoverflow上的其他人和bokeh示例进行调整和适应,但我没有成功生成运行代码。希望一切清楚,您能提供帮助。
Ctrl + Shift + K
,在Chrome中为F12
。 - gherka