手动更改Bokeh p的x范围

2024-04-25 06:12:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在尝试创建一个Bokeh图,我希望能够用一个滑块手动调整X轴的范围。 作为一个新手,我只做到了这一点,尽管我研究了这个课题,我还是没能解决这个问题。在

这里是我的代码:

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show
output_file("test.html")
x = [x*0.5 for x in range(0, 200)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=600, plot_height=400, x_range=(0, 100))
plot.line('x', 'y', source=source, line_width=2, line_alpha=0.75)
callback = CustomJS(args=dict(x_range=plot.x_range), code="""
    var start = cb_obj.value
    x_range.set({"start": start, "end": start+10})
""")
slider = Slider (start=0, end=90, value=20, step=10)
slider.js_on_change('value', callback)
layout = column(slider, plot)
show(layout)

我最大的问题是理解如何将CustomJS连接到我的绘图中。 我很乐意感谢你的帮助。在


Tags: fromimportsourceplotvaluelinebokehrange