Python sns统计图中的客户类型识别
我有以下图表: 我使用以下代码生成了此图:Python sns统计图中的客户类型识别,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有以下图表: 我使用以下代码生成了此图: temp.gender.value_counts()/len(temp)*100).sort_index().plot(kind="bar", rot=0) for p in ax.patches: ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1)) plt.show() 以下是我的数据样本: customer_id| gend
temp.gender.value_counts()/len(temp)*100).sort_index().plot(kind="bar", rot=0)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
plt.show()
以下是我的数据样本:
customer_id| gender |date_joined
8540 | Female |2018-08-12
8544 | Female |
8540 | Male |2016-07-14
我希望能够看到在男性和女性中,有多少客户是奖励计划的一部分。如果他们有日期加入值,则他们是客户奖励计划的一部分,如果他们没有该列的值,则他们不是该计划的一部分
目标:为了直观显示56%的女性和41%的男性,有多少是奖励计划的一部分或在加入日期中有值?可以通过“加入数据”列过滤数据框,并用于生成所需的绘图:
将熊猫作为pd导入
将numpy作为np导入
将matplotlib.pyplot作为plt导入
#生成一些动态测试数据;因为确切的日期并不重要,所以只需使用两个日期即可
N=5000
性别=np.随机选择(['男性','女性'],N)
数据加入=np.随机选择(['2018-01-01','2016-01-01','',N)
temp=pd.DataFrame({'gender':gender,'data_joined':data_joined})
ax=(临时性别值计数()/len(临时)*100).排序索引().绘图(kind=“bar”,rot=0,color='lightblue',label='customer')
ax=(temp[temp['data\u joined']!='')。性别。数值计数()/len(temp)*100。排序索引()。绘图(kind=“bar”,rot=0,color='crimson',label='奖励计划',ax=ax)
对于ax.patches中的p:
ax.annotate(“{.2f}%.”格式(p.get_height()),(p.get_x()+p.get_width()/2,p.get_height()+0.5),ha='center',va='bottom')
ylims=plt.ylim()
plt.ylim(0,ylims[1]+3)#为标签留出空间
plt.legend()
plt.show()
这个答案有用吗?