Python 3.x Bokeh调整图示符';上垫

Python 3.x Bokeh调整图示符';上垫,python-3.x,bokeh,Python 3.x,Bokeh,我有一个使用vbar glyph方法用Bokeh制作的多条形图。绘图很好,但当我尝试添加标签时,标签被“推”到图形之外,无法读取。有没有办法调整字形和图形之间的顶部填充 这是我正在使用的代码,如果你渲染它,问题很明显 categories = ['1', '2', '3', '4', '5'] sets = ['full', 'train', 'test'] data = { 'x' : categories, 'full' : full_dset_count, 'tr

我有一个使用vbar glyph方法用Bokeh制作的多条形图。绘图很好,但当我尝试添加标签时,标签被“推”到图形之外,无法读取。有没有办法调整字形和图形之间的顶部填充

这是我正在使用的代码,如果你渲染它,问题很明显

categories = ['1', '2', '3', '4', '5']
sets = ['full', 'train', 'test']

data = {
    'x' : categories,
    'full' : full_dset_count,
    'train' : train_dset_count,
    'test' : test_dset_count,
    'empty':['']*5
}

x = [ (cat, set_) for cat in categories for set_ in sets ]
counts = sum( zip( data['full'], data['train'], data['test'] ), () )
colors = ['#3cba54', "#f4c20d", "#db3236"]*5

full_p = sum(zip( round(data['full'], 4), data['empty'], data['empty']), ())
train_p = sum(zip( data['empty'], round(data['train'], 4), data['empty']), ())
test_p = sum(zip( data['empty'], data['empty'], round(data['test'], 4)), ())


source = ColumnDataSource(data=dict(
    x=x, counts=counts, colors=colors,
    full_p=full_p, train_p=train_p, test_p=test_p

))

plt = figure(
    x_range=FactorRange(*x),
    plot_height=500,
    plot_width=900,
    title="Distribuzione dei valori delle recensioni per l'insieme totale, di training e di test",
    x_axis_label="Valore delle recensioni",
    y_axis_label="Percentuale delle osservazioni"
)

plt.vbar(
    x='x',
    top='counts',
    width=0.5,
    color='colors',
    source=source    
)

plt.y_range.start = 0
plt.x_range.range_padding = 0.1
plt.xaxis.major_label_orientation = 1
plt.xgrid.grid_line_color = None

full_labels = LabelSet(
    x='x', y='counts', text='full_p', level='glyph',
    x_offset=-30, y_offset=0, source=source, render_mode='canvas'
)
train_labels = LabelSet(
    x='x', y='counts', text='train_p', level='glyph',
    x_offset=-20, y_offset=15, source=source, render_mode='canvas'
)

test_labels = LabelSet(
    x='x', y='counts', text='test_p', level='glyph',
    x_offset=-10, y_offset=30, source=source, render_mode='canvas'
)

plt.add_layout(full_labels)
plt.add_layout(train_labels)
plt.add_layout(test_labels)

show(plt)

full\u dset\u count
train\u dset\u count
test\u dset\u count
的值几乎相同。为了重现该问题,可以使用以下值:
[7.23934138、9.58753275、18.32419776、35.79029432、29.05863379]
您可以调整它,但只能在数据单位中使用它-您已经在使用
plt.X_range.range_padding的X范围中使用它。
这种方法的一个问题是,它不适用于所有标签和打印尺寸,因为这些尺寸是以屏幕单位表示的,而不是以数据单位表示的。如果你能提前设置这些尺寸,并确保标签合适,那就没什么大不了的


另一种方法是使用两个标签集,而不是每个标签集。其中一套将用于酒吧外的标签,如果它们可以放在那里的话。另一组将用于在酒吧内的标签-他们被移动到那里的酒吧太高。类似于Matplotlib文档中此图像的处理方式:

谢谢,我的解决方法与此类似。我定义了3个标签,只定义了一个组件,其余的都是空字符串,并为每个标签指定了不同的偏移量。谢谢你的回答!