I have a horizontal barplot, for example, a simplified version of the example from the seaborn documentation: https://seaborn.pydata.org/examples/horizontal_barplot.html
import seaborn as sns
import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize=(6, 15))
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
sns.barplot(x="total", y="abbrev", data=crashes,
label="Total", color="b")
ax.set(xlim=(0, 24), ylabel="",
xlabel="Automobile collisions per billion miles")
plt.show()
How can I get the bars labeled with the value for each bar?
I tried this approach for vertical bars ( How to add percentages on top of bars in seaborn?) , but it doesn't seem to work. Changing height to width doesn't have the effect I assumed it would.
for p in ax.patches:
height = p.get_width()
ax.text(p.get_y()+p.get_height()/2.,
height + 3,
'{:1.2f}'.format(height),
ha="center")
I'm assuming the horizontal plot works differently?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…