散景调整字形的顶部填充

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

我有一个使用vbar字形方法由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_dset_counttrain_dset_counttest_dset_count的值几乎相同。为了重现该问题,可以使用以下值:[ 7.23934138, 9.58753275, 18.32419776, 35.79029432, 29.05863379]

python-3.x bokeh
1个回答
0
投票

您可以调整它,但只能以数据单位进行调整-您已经在plt.x_range.range_padding的X范围内使用它。这种方法的一个问题-它不适用于所有标签和绘图尺寸,因为这些尺寸以屏幕单位而不是数据单位为单位。如果您可以预先设置这些大小并确保标签合适,那么没什么大不了的。

另一种方法是使用2个标签集而不是每个标签集。一组将用于条形外面的标签,只要它们可以适合在那里。另一组用于条形图内的标签-将它们移到那里以用于过高的条形图。与Matplotlib文档中的图像处理类似:[enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.