小编Bri*_*ald的帖子

如何使用 bar_label 的 fmt 选项将 % 添加到注释

我正在尝试在 Matplotlib 中使用新的 bar_label 选项,但无法找到在标签值后附加文本(例如 '%')的方法。以前,使用 ax.text 我可以使用 f 字符串,但我找不到通过 bar-label 方法使用 f 字符串的方法。

fig, ax = plt.subplots(1, 1, figsize=(12,8))
hbars = ax.barh(wash_needs.index, wash_needs.values, color='#2a87c8')
ax.tick_params(axis='x', rotation=0)

# previously I used this approach to add labels 
#for i, v in enumerate(wash_needs):
#    ax.text(v +3, i, str(f"{v/temp:.0%}"), color='black', ha='right', va='center')

ax.bar_label(hbars, fmt='%.2f', padding=3) # this adds a label but I can't find a way to append a '%' after the number
    
plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib bar-chart

5
推荐指数
1
解决办法
7565
查看次数

标签 统计

bar-chart ×1

matplotlib ×1

python ×1