Bri*_*ald 5 python matplotlib bar-chart
我正在尝试在 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)
Bri*_*ald 13
我找到了一种将“%”附加到标签数字的方法 - 添加一个额外的“ %% ”
ax.bar_label(hbars, fmt='%.2f%%', padding=3)
Run Code Online (Sandbox Code Playgroud)
ax.bar_label(hbars, fmt='%.2f%%', padding=3)
Run Code Online (Sandbox Code Playgroud)