gpt4 book ai didi

python - Bokeh 100% 堆叠条形图

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:13 24 4
gpt4 key购买 nike

Bokeh 中制作 100% 堆叠条形图的最简单方法是什么?例如,假设我有以下列

S    P
34 65
23 44
12 81
9 23

excel 中,我可以很容易地绘制这种类型的图,因此我会得到如下内容:

enter image description here

但是我想进行一些交互(比如在悬停时显示值)因此我想在 Bokeh 中制作这种图。我是 Bokeh 的初学者,我还没有找到与此类似的示例。那么,最好的方法是什么?

最佳答案

数据整理

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

获得正确的比例和标签高度

    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 启动

笔记本

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

情节创作

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)

enter image description here

之后您可以添加 HoverTool 并更正 ticksgrid

关于python - Bokeh 100% 堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com