在Jupyter/Python中使用bokeh绘制交互式饼图

问题描述:

我是Bokeh的新手,我非常感谢在弄清楚如何使用Bokeh在Jupyer/Python中绘制简单的交互式饼图方面所提供的帮助.我打算在Bokeh中使用带有Python函数的CustomJS",如页面底部所述此处.饼图由两个带有滑块的条目组成,该滑块可在(v1 + v2)的圆形形状内更改一个饼图"v2"的形状.我试图按照bokeh网站上的示例显示正弦图的交互性,但是我无法使其与饼图配合使用.任何帮助将不胜感激.以下是我在Jupyter笔记本中使用的代码块.

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)

如果您想交互式地更新内容,那么使用

If you want to interactively update things, then you will be better off using the bokeh.plotting API. For some fairly uninteresting technical reasons, the bokeh.charts API (including Donut) is not well-suited for use cases that require updating things in place.

bokeh.plotting中,有一个您可以使用wedge字形方法绘制饼图.这是编写的完整示例(使用Bokeh 0.12.5),该示例使用滑块更新饼图:

With bokeh.plotting there is a wedge glyph method that you can use to draw pie charts. Here is a complete example written (using Bokeh 0.12.5) that updates a pie chart with a slider:

from math import pi

from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import figure

output_file("pie.html")

source = ColumnDataSource(data=dict(
    start=[0, 0.2], end=[0.2, 2*pi], color=['firebrick', 'navy']
))

plot = figure()
plot.wedge(x=0, y=0, start_angle='start', end_angle='end', radius=1,
        color='color', alpha=0.6, source=source)

slider = Slider(start=.1, end=1., value=.2, step=.1, title="delta-V")

def update(source=source, slider=slider, window=None):
    data = source.data
    data['end'][0] = slider.value
    source.trigger('change')

slider.js_on_change('value', CustomJS.from_py_func(update))

show(column(slider, plot))

它比Donut版本更为冗长,但是python端和JS端的数据结构之间的关系更加清晰和直接.

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.