【发布时间】:2021-08-06 14:20:00
【问题描述】:
我正在尝试在 Matplotlib 中使用新的 bar_label 选项,但无法找到附加文本的方法,例如'%' 在标签值之后。以前,使用 ax.text 我可以使用 f-strings,但我找不到通过 bar-label 方法使用 f-strings 的方法。
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()
【问题讨论】:
-
stack bar plot in matplotlib and add label to each section 和 How to add multiple annotations to a barplot 展示了如何使用带有
label参数的 f 字符串自定义注释。
标签: python matplotlib bar-chart