You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Bokeh多组条形图标签被挤出,如何调整Glyph顶部内边距?

解决Bokeh多组条形图标签被挤出的问题

嘿,我明白你遇到的困扰了——当给密集的多组条形图添加标签时,很容易出现标签超出图表可视区域的情况。咱们可以通过几个简单的调整来解决这个问题:

方法1:手动设置Y轴范围上限

这是最直接的解决方案。你的数据最大值接近36,默认的Y轴范围会刚好贴合这个最大值,加上标签的y_offset就会超出顶部。我们可以把Y轴的终点设得更高一些,给标签留出足够空间:

# 在设置plt.y_range.start = 0之后添加
plt.y_range.end = 40  # 比数据最大值大4-5左右,根据你的标签偏移量调整

方法2:调整图表顶部边距

如果你不想改变Y轴的显示范围,还可以通过增加图表的顶部内边距来容纳标签:

# 在创建figure的时候添加margin参数
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",
    margin=(50, 20, 20, 20)  # 顺序是(top, right, bottom, left)
)

方法3:优化标签的偏移量

你的test_labels设置了y_offset=30,这个值可能偏大,适当减小可以让标签更靠近条形,避免超出顶部:

test_labels = LabelSet(
    x='x',
    y='counts',
    text='test_p',
    level='glyph',
    x_offset=-10,
    y_offset=20,  # 减小偏移量
    source=source,
    render_mode='canvas'
)

修改后的完整代码

把这些调整整合到你的代码里,应该就能正常显示标签了:

from bokeh.io import show
from bokeh.models import ColumnDataSource, FactorRange, LabelSet
from bokeh.plotting import figure

# 复现用的数据
full_dset_count = [7.23934138, 9.58753275, 18.32419776, 35.79029432, 29.05863379]
train_dset_count = [7.23934138, 9.58753275, 18.32419776, 35.79029432, 29.05863379]
test_dset_count = [7.23934138, 9.58753275, 18.32419776, 35.79029432, 29.05863379]

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",
    margin=(50, 20, 20, 20)  # 添加顶部边距
)
plt.vbar(
    x='x',
    top='counts',
    width=0.5,
    color='colors',
    source=source
)
plt.y_range.start = 0
plt.y_range.end = 40  # 调整Y轴上限,给标签留空间
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=20,  # 减小偏移量
    source=source,
    render_mode='canvas'
)
plt.add_layout(full_labels)
plt.add_layout(train_labels)
plt.add_layout(test_labels)
show(plt)

这些调整组合起来,就能让标签完全显示在图表内部,不会被挤出啦!

内容的提问来源于stack exchange,提问作者MFranc

火山引擎 最新活动