本文介绍了Seborn:如何在计数图中显示数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我有数据显示某个性别的人是否有孩子(1)或没有(0)。这在countplot
中显示。但是,我希望将值放在列中,或者放在列的顶部。我如何才能做到这一点?
df = pd.DataFrame({"Gender":["Male", "Male", "Female", "Male", "Female", "Female"],
"children":["0", "1", "0", "0", "1", "1"]})
sns.countplot(x="Gender", hue="children", data=df, palette="binary")
推荐答案
只需循环访问countplot
返回的所有补丁。然后,在给定每个面片的x位置和高度的情况下创建文本。我选择了白色并添加了换行符,以在栏顶部正下方显示数字。
部分代码:
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
df = pd.DataFrame({"Gender":["Male", "Male", "Female", "Male", "Female", "Female", "Male", "Male", "Female", "Male", "Female", "Female"],
"Children":["0", "1", "0", "0", "1", "1", "0", "1", "0", "0", "1", "1"]})
ax = sns.countplot(x="Gender", hue="Children", data=df, palette="plasma")
ax.set_title('Survival in terms of gender', fontsize=20)
for p in ax.patches:
ax.annotate(f'
{p.get_height()}', (p.get_x()+0.2, p.get_height()), ha='center', va='top', color='white', size=18)
plt.show()
这篇关于Seborn:如何在计数图中显示数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!