[Python] ratio plot

2020. 9. 23. 17:25분석 Python/Visualization

tr = np.random.choice(["male","female"],size = 1000, replace=True,p=[0.9,0.1]).tolist()
te = np.random.choice(["male","female"],size = 1000, replace=True,p=[0.4,0.6]).tolist()
a =  tr + te 
aa = pd.DataFrame({"sex" :a , "var" : ["fraud"] *len(tr) + ["non-fraud"] *len(te) })
aa.groupby("var")['sex'].value_counts(normalize=True).unstack().plot(kind="bar", 
                                                                          legend=True , 
                                                                          stacked= True)
plt.xlabel("")
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
       ncol=2, borderaxespad=0.)
plt.savefig("./cat_1.pdf")
plt.show()
728x90