简体   繁体   English

如何将观测值的数量添加到Seaborn barplot中?

[英]How can I add the number of observations to a Seaborn barplot?

I want to add the number of observations to Seaborn barplots. 我想将观测值添加到Seaborn的条形图上。 I created a barplot with four bars that represent percentages on the y axis. 我创建了一个带有四个表示y轴百分比的条形图的条形图。 I want to add a label on each bar showing the number of observations. 我想在每个栏上添加一个标签,以显示观察次数。

In my code, the first block creates the barplot. 在我的代码中,第一个块创建了barplot。

I created the second two blocks of code from examples that I found elsewhere. 我根据在其他地方找到的示例创建了后两段代码。 I get an error message pointing to the row beginning with "medians," and the message says: AttributeError: 'float' object has no attribute 'values' 我收到一条错误消息,该错误消息指向以“ medians”开头的行,并且消息显示:AttributeError:“ float”对象没有属性“ values”

sns.set_style("whitegrid")
ax = sns.barplot(x=barplot_x, y="trump_margin_pct", 
data=mean_analysis)
sns.palplot(sns.diverging_palette(240, 0))
ax.set(xlabel='Strength of Candidate Support', ylabel='Average Trump 
Margin of Victory/(Loss) (in %)')  
ax.set_title('Average Strength of Candidate Support Across Groups of 
Counties, 2016')

# Calculate number of obs per group & median to position labels
medians = mean_analysis['trump_margin_pct'].median().values
nobs = mean_analysis['trump_margin_pct'].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]

# Add it to the plot
pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
     ax.text(pos[tick], medians[tick] + 0.03, nobs[tick],
horizontalalignment='center', size='x-small', color='w', 
weight='semibold')

Your approach is almost right. 您的方法几乎是正确的。 However, you calculate the median and the number of observations over the whole data mean_analysis['trump_margin_pct'] and not over groups. 但是,您要计算整个数据mean_analysis['trump_margin_pct']而非整个组的观测值的中位数和数量。 This causes your error. 这会导致您的错误。 You can use groupby to calculate over groups. 您可以使用groupby来计算分组。

Median: 中位数:
Simply add groupby to calculate your median. 只需添加groupby即可计算出中位数。

medians = mean_analysis.groupby(['barplot_x'])['trump_margin_pct'].median().values


Number of obs: Obs数量:
For the number of obervations you have to calculate the aggregated value counts that are grouped. 对于观察数,您必须计算分组的合计值计数。 This is how you can do this. 这就是您可以执行的操作。

nobs = mean_analysis.groupby(['barplot_x'])['trump_margin_pct'].agg(['count'])
nobs = ["n: " + str(i) for s in nobs.values for i in s]


Example: 例:
I used some dummy data to recreate your example. 我使用了一些虚拟数据来重新创建您的示例。

import seaborn as sns

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.barplot(x="day", y="total_bill", data=tips)
ax.set(xlabel='Strength of Candidate Support', ylabel='Average Trump Margin of Victory/(Loss) (in %)')  
ax.set_title('Average Strength of Candidate Support Across Groups of Counties, 2016')

medians = tips.groupby(['day'])['total_bill'].median().values
nobs = tips.groupby(['day'])['total_bill'].agg(['count'])
nobs = ["n: " + str(i) for s in nobs.values for i in s]

pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-small', color='w', weight='semibold')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM