【发布时间】:2020-09-12 02:13:41
【问题描述】:
我有以下代码在 seaborn 中生成条形图
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print(df):
A B C D
0 15 21 13 5
1 14 94 99 14
2 11 11 13 69
3 27 90 37 6
4 51 93 92 24
.. .. .. .. ..
95 45 40 85 62
96 44 48 61 43
97 39 66 72 72
98 51 97 17 32
99 51 42 29 15
probbins = [0,10,20,30,40,50,60,70,80,90,100]
df['Groups'] = pd.cut(df['D'],bins=probbins)
plt.figure(figsize=(15,6))
chart = sns.barplot(x=df['Groups'], y=df['C'],estimator=sum,ci=None)
chart.set_title('Profit/Loss')
chart.set_xticklabels(chart.get_xticklabels(), rotation=30)
plt.show()
这给了我:
我怎样才能简单地为这个图添加数据标签?任何帮助将不胜感激!
【问题讨论】:
-
Seaborn Barplot - Displaying Values这里有和你一样的问题和答案。
-
感谢@r-beginners。我很难理解
for index, row in groupedvalues.iterrows(): g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")将如何适用于我的案例。 -
它的作用是一次一行地从 DF 中获取值以显示该值。由于“g”指向条形图,因此向该条形图添加文本的过程是在循环中完成的。
-
另一个解决方案here,虽然它是针对
barh/ -
由于您似乎是 Stack Overflow 的新手,您应该阅读How to create a Minimal, Complete, and Verifiable example
标签: python pandas matplotlib seaborn