使用RangeSlift更改轴范围

2024-04-25 13:26:25 发布

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

我用bokeh创建了一个绘图,现在正在尝试添加一些小部件。目前我想用RangeSlider改变x轴,但我想我有一些问题与回调。随函附上代码:

import pandas as pd
import numpy as np
import bokeh.plotting as bp
from bokeh.models import CustomJS
from bokeh.models.widgets import RangeSlider
from bokeh.layouts import row, widgetbox
from bokeh.plotting import figure, show, output_file


df = pd.DataFrame(np.random.randint(0,30,size=(100, 3)), columns=list('ABC'))
df.index.name ='Numbers'
dt = bp.ColumnDataSource(df)

output_file("Plot.html")

p = figure(title="Example", 
       x_axis_label = "Number", y_axis_label = "Temperature in C",
       x_range=(0,100))

p.line(source=dt, x='Numbers', y='A', color='blue', legend='Line A')
p.line(source=dt, x='Numbers', y='B', color='red', legend='Line B')
p.line(source=dt, x='Numbers', y='C', color='green', legend='Line C')
p.legend.click_policy="hide"

callback = CustomJS(args=dict(p=p), code="""
                      var a = cb_obj.value;
                      p.x_range = a;
                      p.change.emit();
                      """)

range_slider = RangeSlider(start=0, end=100,value=(2,75), step=1, title="Test")
range_slider.js_on_change('value', callback)


layout = row(p,widgetbox(range_slider))
show(layout)

提前谢谢!在


Tags: fromimportsourcedfvalueaslinedt
1条回答
网友
1楼 · 发布于 2024-04-25 13:26:25

您必须在范围内分别设置start和{}:

callback = CustomJS(args=dict(p=p), code="""
    var a = cb_obj.value;
    p.x_range.start = a[0];
    p.x_range.end = a[1];
""")

另外,对emit的调用是不必要的。在

相关问题 更多 >