如何用seaborn.distplot绘制百分比?

问题描述

是否可以绘制百分比而不是distplot上的计数?

                  Details    Transaction
0  Date: 11:20 01/01/2020  Debit $100.50
1  Date: 13:15 01/02/2020  Credit $50.00

Image generated by the code

解决方法

您可以使用norm_hist = True

来自documentation

norm_hist :bool,可选

如果为True,则直方图高度显示的是密度而不是计数。 如果绘制了KDE或拟合密度,则暗示这一点。

,

您可以选择一个柱状图,并设置一个以百分比定义归一化的估算器:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame(dict(x=np.random.poisson(10,1_000)))
ax = sns.barplot(x="x",y="x",data=df,palette=["teal","crimson"],estimator=lambda x: len(x) / len(df) * 100
                 )
ax.set(xlabel="tenure")
ax.set(ylabel="Percent")

plt.show()

给予:

enter image description here