简体   繁体   English

散景100%堆积条形图

[英]Bokeh 100% Stacked Bar Chart

What is the easiest way to make a 100% Stacked Bar Chart in Bokeh ?, for example, suppose I have the following columns 例如,假设我有以下几列,那么在Bokeh制作100%堆积条形图的最简单方法是什么?

S    P
34   65
23   44
12   81
 9   23

In excel I can make this type of plot very easy, so I would obtain something like this: excel我可以使这种类型的绘图变得非常容易,因此我将获得如下内容:

在此处输入图片说明

However I want to have some interaction (like showing the values on hover) thus I want to make this kind of plot in Bokeh . 但是我想进行一些交互(例如显示悬停时的值),因此我想在Bokeh进行这种绘图。 I'm a beginner with Bokeh and I haven't found any example similar to this. 我是Bokeh的初学者,但没有找到类似的示例。 So, what would be the best approach? 那么,什么是最好的方法?

Data wrangling 数据争吵

df_comb = df.join(df.divide(df.sum(axis=1), axis=0), rsuffix='_w').join(df.divide(df.sum(axis=1) * 2, axis=0), rsuffix='_w_labelheights')
df_comb['P_w_labelheights'] += df_comb['S_w']
df_comb

to get the correct proportions and label heights 获得正确的比例和标签高度

    S   P   S_w         P_w         S_w_labelheights    P_w_labelheights
0   34  65  0.343434    0.656566    0.171717    0.671717
1   23  44  0.343284    0.656716    0.171642    0.671642
2   12  81  0.129032    0.870968    0.064516    0.564516
3   9   23  0.281250    0.718750    0.140625    0.640625

Bokeh initiation 散景萌生

for a notebook 用于笔记本

from bokeh.models import ColumnDataSource
from bokeh.plotting import show, output_notebook, figure as bf
output_notebook()

Plot creation 情节创作

f = bf()
source = ColumnDataSource(df_comb)

s = f.vbar(x='index', bottom=0, top='S_w', width=0.5, source=source)
p = f.vbar(x='index', bottom='S_w', top=1, width=0.5, source=source, color='orange')

s_label = f.text(x='index', y='S_w_labelheights', source=source, text='S')
p_label = f.text(x='index', y='P_w_labelheights', source=source, text='P')

show(f)

在此处输入图片说明

You can afterwards add the HoverTool and correct the ticks and grid 之后,您可以添加HoverTool并更正ticksgrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM