【发布时间】:2022-08-20 04:15:38
【问题描述】:
这是我关于 Stack Overflow 的第一个问题,我希望我做对了。 我正在开发 Jupyter Notebook、Python 3.9 和 Bokeh 2.4.3,但我对 JavaScript 一无所知。 我已经构建了一些工作代码,通过两个下拉菜单选择要检查的列作为 x 和 y 从数据框中绘制数据:
from bokeh.io import show, output_notebook
from bokeh.models import ColumnDataSource, Select, Column, Row, CustomJS, PreText
from bokeh.plotting import figure
import pandas as pd
output_notebook()
data = {\'moo\': [1, 2, 3, 4],
\'woof\': [20, 21, 19, 18],
\'purr\': [33, 45, 12, 16]}
df = pd.DataFrame(data)
source = ColumnDataSource(df)
p = figure(plot_height=500, plot_width=500, x_axis_label=\"moo\", y_axis_label=\"woof\")
r = p.circle(x=\'moo\', y=\'woof\', source=source)
selecty = Select(value=\'woof\', title=\'y-axis\', options=list(df.columns))
selectx = Select(value=\'moo\', title=\'x-axis\', options=list(df.columns))
cby = CustomJS(args=dict(r=r, select=selecty, yaxis=p.yaxis), code=\"\"\"
// tell the glyph which field of the source y should refer to
r.glyph.y.field = select.value;
// change axis label accordingly
yaxis[0].axis_label = select.value;
// manually trigger change event to re-render
r.glyph.change.emit()
\"\"\")
cbx = CustomJS(args=dict(r=r, select=selectx, xaxis=p.xaxis), code=\"\"\"
// tell the glyph which field of the source y should refer to
r.glyph.x.field = select.value;
// change axis label accordingly
xaxis[0].axis_label = select.value;
// manually trigger change event to re-render
r.glyph.change.emit()
\"\"\")
selecty.js_on_change(\'value\', cby)
selectx.js_on_change(\'value\', cbx)
show(Row(Column(selecty, selectx), p))
到目前为止,一切都很好。现在我想添加从以下代码获得的相关矩阵中获取的交互式文本:corrMatrix = df.corr() 这样,例如,当我选择将moo 绘制为横坐标,woof 绘制为纵坐标时,代码将在菜单正下方打印-0.80(因此,在绘图之外)。我想我应该使用bokeh.models 中的PreText,但我不知道应该使用的JS 代码。
提前致谢。
标签: python-3.x callback bokeh jupyter-lab interactive