如何使用bokeh从回调更新轴标签?

2024-03-28 12:19:26 发布

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

有关于如何从回调更新轴标签的线索吗? 同时更改工具提示。 另外,如果有一种更简单/更好的方法来编码绘图之间的交互,请告诉我

enter image description here

这是一个最小的例子

values = list('ABCDFG')

df = pd.DataFrame(np.random.randint(0,100,size=(30, 6)), columns=values)

varName1 = 'A'
varName2 = 'B'

df['x'] = df[varName1]
df['y'] = df[varName2]

source = ColumnDataSource(df)

p0 = figure(tools='pan, wheel_zoom, box_select, lasso_select', plot_width=400, plot_height=400, 
            active_scroll="wheel_zoom",
            x_axis_label=varName1, y_axis_label=varName2,
            tooltips=[ (varName1, "@" + varName1),
                       (varName2, "@" + varName2)
                     ],
            title= varName1 + " vs " + varName2)

circles = p0.circle('x', 'y', source=source,
                    size=15, line_color="navy", fill_color="navy", fill_alpha=0.3)

select1 = Select(title="Variable 1:", value=varName1, width=100, options=values)
select2 = Select(title="Variable 2:", value=varName2, width=100, options=values)

changeVariables = CustomJS(args=dict(plot=p0, source=source, select1=select1, select2=select2, ax1=p0.xaxis, ax2=p0.yaxis), code="""
    var varName1 = select1.value;
    var varName2 = select2.value;
    plot.title.text = varName1 + " vs " + varName2;
    ax1[0].axis_label = varName1;
    ax2[0].axis_label = varName2; 
    source.data['x'] = source.data[varName1];
    source.data['y'] = source.data[varName2];
    source.change.emit();
""")

select1.js_on_change("value", changeVariables)
select2.js_on_change("value", changeVariables)

show(row(p0, select1, select2))

Tags: sourcedfdataplottitlevaluewidthlabel