I have been following some examples here at stackoverflow for updating my plot with a 'select' widget. When I run the .py file through anaconda shell, I get to see the plot and the 'select' widget. Somehow my plot does not update the plot though. I must say the dataset has a count of around 11000 rows(I don't know if this is relevant). I saw a topic where turning the dataframe to dictionaries helped someone to get the interaction working. So I did that with the following code:
from bokeh.layouts import row, column, widgetbox
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.models.widgets import Select
from bokeh.io import curdoc, show
df = pd.read_excel('data.xlsx')
d1 = df.to_dict()
d2 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'compliment'].to_dict()
d3 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'complaint'].to_dict()
d4 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'infrastructure'].to_dict()
d5 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'autority'].to_dict()
d6 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'finance'].to_dict()
d7 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'english'].to_dict()
d8 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'danger'].to_dict()
d9 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'health'].to_dict()
d10 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'sport'].to_dict()
d11 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'remaining'].to_dict()
Now that I have made the dictionaries, I made the plot with the following code:
source = ColumnDataSource(df)
p = figure()
r = p.circle(x='Polarity', y='Subjectivity',
source = source)
select = Select(title="Subject", options=['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11'])
def update_plot(attr, old, new):
if select.value == 'd1':
newSource = d1
if select.value == 'd2':
newSource = d2
if select.value == 'd3':
newSource = d3
if select.value == 'd4':
newSource = d4
if select.value == 'd5':
newSource = d5
if select.value == 'd6':
newSource = d6
if select.value == 'd7':
newSource = d7
if select.value == 'd8':
newSource = d8
if select.value == 'd9':
newSource = d9
if select.value == 'd10':
newSource = d10
if select.value == 'd11':
newSource = d11
source.data = newSource
select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
curdoc().add_root(layout)
After many different attempts i still failed with getting the interaction working. What am I doing wrong?
output_fileandshow. Those are typically used with standalone Bokeh documents that are static HTML and JS and do not work with real Pythonon_changecallbacks. Are you running this like a normal python script or are you running it with e.g.bokeh serve --show app.py? - bigreddot