添加百分比到Bokeh甜甜圈图表

时间:2016-09-22 06:08:59

标签: python-3.x bokeh

我正在使用Bokeh Donut图表来创建饼图。我正在使用悬停工具显示鼠标悬停时每个切片的原始大小和类别。我还希望悬停工具显示每个切片的整个百分比。

这可能吗?我的理解是,目前有一个开放的拉取请求,专注于构建向悬停工具添加任意附加信息的功能,但是有任何类型的当前工作可以让我实现这一目标吗?我一直试图尝试手动添加列到悬停工具数据源(ColumnDataSource?)的方法,但还没有成功。目前,悬停的Percentage工具提示刚出现???

#limit data 
tension = rrima.loc[:,['community_tension','tension_pct']]

#analysis of community tensions
tensions = Donut(tension,label='community_tension',values='community_tension',agg='count',
                 plot_height=plot_height,plot_width=plot_width,title='Reported Community Tensions')


hover = HoverTool(point_policy='follow_mouse')
hover.tooltips = [('Survey Responses','@values'),('Percentage','@tension_pct'),('Community Tensions','@community_tension')]
tensions.add_tools(hover)

1 个答案:

答案 0 :(得分:0)

不推荐使用旧的Donut API(包括bokeh.plotting),并于去年将其完全删除。但是,您可以使用稳定的from collections import Counter from math import pi import pandas as pd from bokeh.io import show from bokeh.palettes import Category20c from bokeh.plotting import figure from bokeh.transform import cumsum # Some example data x = Counter({ 'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63, 'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32, 'France': 31, 'Taiwan': 31, 'Spain': 29 }) data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(index=str, columns={0:'value', 'index':'country'}) data['percent'] = data['value'] / sum(x.values()) * 100 data['angle'] = data['value'] / sum(x.values()) * 2*pi data['color'] = Category20c[len(x)] p = figure(plot_height=350, tooltips="@country: @percent{0.2f} %") p.wedge(x=0, y=1, radius=0.4, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend='country', source=data) show(p) API来完成此操作。这是使用Bokeh 0.13的完整示例:

tbl_Rota

enter image description here