顶部的散景标签集 - doged 时位于栏的中间。如何?

问题描述 投票:0回答:0

我无法为我的标签集提供正确的位置。 我的数据每 10 分钟更改一次,因此一张图表可能有 3 个狗栏(6 栏) 以后会有6个左右。

我将标签放在条形图的顶部并使用 x_offset 设置位置但是当不同数量的图表创建给定的 x_offset 时不正确。

所以,它超过了正确的标准。

我创建了一个示例代码。

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.transform import dodge
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.models import NumeralTickFormatter
from bokeh.transform import transform

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

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

source = ColumnDataSource(data=data)

p = figure(x_range=fruits, y_range=(0, 10), title="Fruit Counts by Year",
           height=500, width= 1500, toolbar_location=None, tools="")

p.vbar(x=dodge('fruits', -0.15, range=p.x_range), top='2015', source=source,
       width=0.3, color="#c9d9d3")

p.vbar(x=dodge('fruits',  0.15,  range=p.x_range), top='2016', source=source,
       width=0.3, color="#718dbf")

formatter = NumeralTickFormatter(format= '00:00:00')
labels = LabelSet( x='fruits',
                   y='2015',
                   text = transform('2015',formatter),
                   source=source,
                   text_color ='black',
                   text_font = 'Consolas',
                   text_align = 'right',
                   text_font_size="10pt",
                   text_font_style = 'bold',                                                                   
                   )
labels1 = LabelSet(x='fruits',
                   y='2016',
                   text=transform('2016',formatter),
                   source=source,
                   text_color ='black',
                   text_font = 'Consolas',
                   text_font_size="10pt",
                   text_font_style = 'bold',                
                   )
p.add_layout(labels)
p.add_layout(labels1)

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)
label bokeh
© www.soinside.com 2019 - 2024. All rights reserved.