I am new to Bokeh and I would really appreciate some help in figuring out how to use Bokeh to plot a simple interactive pie chart in Jupyer/Python. I am planning to use 'CustomJS with a Python function' in Bokeh as explained at the bottom of the page here. The pie chart consists of two entries with a slider that can change the shape of one pie 'v2' inside the circle shape of (v1+v2). I have tried to follow the example in bokeh website that shows the interactivity with a sine plot, but I just cannot get it to work with my pie chart. Any help would be greatly appreciated. Below is the code block I am using inside a Jupyter notebook.
import numpy as np
import matplotlib.pyplot as plt
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show, output_notebook
from bokeh.charts import Donut, show
#output_file('donut.html')
output_notebook()
v1=1
v2=.2
import pandas as pd
data = pd.Series([v1,v2], index = list('ab'))
plot = Figure(plot_width=400, plot_height=400)
plot = Donut(data)
def pie_chart(source=data,window=None,deltav=None):
data = source.data
v2 = deltav.value
#v2 = data['v2']
source.trigger('change')
slider = Slider(start=.1, end=1., value=.2, step=.1, title="delta-V", callback=CustomJS.from_py_func(pie_chart))
callback.args["deltav"] = slider
l = column(slider, plot)
show(l)
I'm using Bokeh 1.0.4 and the answer by bigreddot doesn't work for me. The slider doesn't actually change the plot. I put together a complete working answer that works for me in JupyterLab v0.27.0 using bigreddot's example and this website.
I think I found my answer. Here is the code in case it helps
If you want to interactively update things, then you will be better off using the
bokeh.plotting
API. For some fairly uninteresting technical reasons, thebokeh.charts
API (includingDonut
) is not well-suited for use cases that require updating things in place.With
bokeh.plotting
there is awedge
glyph method that you can use to draw pie charts. Here is a complete example written (using Bokeh0.12.5
) that updates a pie chart with a slider:It's slightly more verbose than the
Donut
version, but the relationship between the data structures on the python side and on the JS side are much more clear and direct.The code from bigreddot didn't work for me. Plot showed but didn't change. Here is my slight modification for anyone that could use it.
All those suggestions helped! Thanks a bunch.
I am back with a little twist to my original question. Now I would like to perform the same interactive plot using bokeh server. I have tweaked the sine plot example with sliders to generate the code below for the donut chart problem of mine. When I serve it up with bokeh serve, I am getting an error with the definition of 'data' in my code. I think I am close but missing a minor step.