how to update the bar chart source ( dataframe ) based on user selection from Bokeh widget

Here is the data frame and i would like to slice the data frame to only plot data for the state user selected.

i am not able to figure how to update the frame without using customJS callbacks

Name
City
State
0
ABC
Abington
PA
32000
1
DEF
Phoenix
AZ
0
2
DEF
Glendale
AZ
13097
3
ABC
Phoenix
AZ
9475
4
EFG
Phoenix
AZ
3975
5
GHI
Phoenix
AZ
5226
6
IHJ
Goodyear
AZ
10949
7
IJK
Crowley
LA
2487
8
KLM
Altamonte Springs
FL
292094
9
ABC
Oak Lawn
IL
37937
10
ABC
Libertyville
IL
16698
11
DEF
Downers Grove
IL
14159
12
DEF
Barrington
IL
9652
13
ABC
Downers Grove
IL
160902
14
EFG
Park Ridge
IL
26916
15
GHI
Hazel Crest
IL
10260
16
IHJ
Chicago
IL
9282
17
IJK
Anchorage
AK
7422

In[2]:

declare imports

import pandas as pd

from bokeh.io import push_notebook, show, output_notebook, output_file

from bokeh.charts import Bar

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.models.widgets import Button, TextInput, Slider, Dropdown, Select

from bokeh.layouts import widgetbox ,row, column

In[3]:

get data

df = pd.read_excel(’…/data.xlsx’)

df.head(19);

In[11]:

function to make a plot

def make_plot(cdf):

plot = Bar(cdf, label = ‘City’, values =‘Metric1’, title = ‘BarPlot’, legend=False, width=800, height=800)

push_notebook()

show(plot, notebook_handle = True)

return plot

function to update data

def update_plot(attr, old, new):

state = select.value

sdf = sdf[cdf[‘State’] == state]

make_plot(sdf)

In[13]:

#create a widget

instead of ipywidget can we use bokeh widget

create a function to update the plot based on the state selected

create a lists to assign to plot

stateslist = df[“State”].unique().tolist();

select = Select(title=“Select Name:”, options= stateslist)

In[15]:

plot it

plot = make_plot(df)

update values usinsg widgets

select.on_change(‘value’,update_plot)

picture the layout of plot and controls on ui

controls = column(select)

curdoc().add_root(row(plot,controls))

In[16]:

In:

Updating a bokeh.chart basically means creating an entirely new chart to replace the old one. For the use-case of updating data in a plot in place, the bokeh.plotting API is generally going to be much preferred. This is because the relationship between the data you start with and the data that the chart shows is more direct and transparent. You can create bars easily now using bokeh.plotting as well:

  http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars

Then your python callback can simply update the .data in a ColumnDataSource, as is demonstrated in many of the existing examples.

Thanks,

Bryan

···

On Mar 6, 2017, at 14:22, NSR Murthy <[email protected]> wrote:

Here is the data frame and i would like to slice the data frame to only plot data for the state user selected.
i am not able to figure how to update the frame without using customJS callbacks

Name City State
0 ABC Abington PA 32000
1 DEF Phoenix AZ 0
2 DEF Glendale AZ 13097
3 ABC Phoenix AZ 9475
4 EFG Phoenix AZ 3975
5 GHI Phoenix AZ 5226
6 IHJ Goodyear AZ 10949
7 IJK Crowley LA 2487
8 KLM Altamonte Springs FL 292094
9 ABC Oak Lawn IL 37937
10 ABC Libertyville IL 16698
11 DEF Downers Grove IL 14159
12 DEF Barrington IL 9652
13 ABC Downers Grove IL 160902
14 EFG Park Ridge IL 26916
15 GHI Hazel Crest IL 10260
16 IHJ Chicago IL 9282
17 IJK Anchorage AK 7422

# In[2]:

# declare imports
import pandas as pd
from bokeh.io import push_notebook, show, output_notebook, output_file
from bokeh.charts import Bar
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Button, TextInput, Slider, Dropdown, Select
from bokeh.layouts import widgetbox ,row, column

# In[3]:

# get data
df = pd.read_excel('../data.xlsx')
df.head(19);

# In[11]:

# function to make a plot

def make_plot(cdf):
    plot = Bar(cdf, label = 'City', values ='Metric1', title = 'BarPlot', legend=False, width=800, height=800)
    push_notebook()
    show(plot, notebook_handle = True)
    return plot

# function to update data

def update_plot(attr, old, new):
    state = select.value
    sdf = sdf[cdf['State'] == state]
    make_plot(sdf)
        
# In[13]:

#create a widget
# instead of ipywidget can we use bokeh widget
# create a function to update the plot based on the state selected
# create a lists to assign to plot
stateslist = df["State"].unique().tolist();
select = Select(title="Select Name:", options= stateslist)

# In[15]:

# plot it
plot = make_plot(df)
# update values usinsg widgets
select.on_change('value',update_plot)
# picture the layout of plot and controls on ui
controls = column(select)
curdoc().add_root(row(plot,controls))

# In[16]:

# In:

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/333f6c23-e007-4121-bb4d-307890f9b1b7%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.