python绘制饼图双层,在Jupyter / Python中使用bokeh绘制交互式饼图

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.

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))

bJPuF.gif

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值