gpt4 book ai didi

python - 如何防止 HBar 在 Bokeh 中重叠?

转载 作者:行者123 更新时间:2023-12-01 08:59:51 25 4
gpt4 key购买 nike

我有一些已经运行了一段时间的“分类数据”组。其中一些事情也是同时进行的。我想在 Bokeh Python 的时间轴上显示这些类别。

但是,最初的尝试并没有成功: enter image description here

之后,我尝试抖动它们,但它创建了一些非常困惑和无组织的东西(可能是抖动的点,我只是没有正确使用它。) enter image description here

理想情况下,我只想要一种方法,允许我在每个类别中交错这些项目,但我不确定如何做。

如有任何建议,我们将不胜感激!

最佳答案

你不想抖动,你想视觉躲闪,也就是demonstrated in the documentation .

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']

data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 3, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

p = figure(x_range=fruits, y_range=(0, 10), plot_height=250)
p.vbar(x=dodge('fruits', -0.25, range=p.x_range), top='2015', width=0.2, source=source, color="#c9d9d3")
p.vbar(x=dodge('fruits', 0.0, range=p.x_range), top='2016', width=0.2, source=source, color="#718dbf")
p.vbar(x=dodge('fruits', 0.25, range=p.x_range), top='2017', width=0.2, source=source, color="#e84d60")

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None

show(p)

enter image description here

或者,您可以使用显式 Categorical Offsets :

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']

offsets = [-0.5, -0.2, 0.0, 0.3, 0.1, 0.3]

# This results in [ ['Apples', -0.5], ['Pears', -0.2], ... ]
x = list(zip(fruits, offsets))

p.vbar(x=x, top=[5, 3, 4, 2, 4, 6], width=0.8)

enter image description here

关于python - 如何防止 HBar 在 Bokeh 中重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540568/

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