如何防止我的HBar在Bokeh中重叠?

时间:2018-09-27 15:36:42

标签: python visualization bokeh

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

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

在那之后,我尝试使它们抖动,但是它造成了一些混乱且杂乱无章的内容(可能是抖动点,我只是没有正确使用。) enter image description here

理想情况下,我只希望有一种方法可以让我在每个类别中错开这些项目,但我不确定如何做到。

任何建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

您不想抖动,想在视觉上躲闪,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

相关问题