I built a scatterplot whose datapoint coordinates are changed by two dropdown menus. What I cannot do is adding some text that changes everytime I change what I plot. The text is provided by a correlation matrix, so that when I plot column a vs column b the following text should appear: “the correlation is: 0.80”. Is it even possible to do that? Usually I can access the correlation matrix elements with either numerical indices or column names (e.g. corr_mat[0][2] or corr_mat[‘moo’][‘woof’]), but this is lost passing to a ColumnDataSource.
Here is the code I’ve written so far:
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)
corr_mat = df.corr()
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)
# I added this part just to show what I would like to see as a final product.
# The number should be changing everytime I change the selection in dropdown menus
q = PreText(text = 'correlation: ' + str(corr_mat[selectx.value][selecty.value]))
# End of explanatory code
show(Row(Column(selecty, selectx, q), p))
BTW I asked the same queston here (the code is slightly different):
Thanks in advance