如何使用seaborn distplot/histplot/Dislot绘制百分比

2024-06-15 00:50:50 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法在distplot上绘制百分比而不是计数

ax = sns.FacetGrid(telcom, hue='Churn', palette=["teal", "crimson"], size=5, aspect=1)
ax = ax.map(sns.distplot, "tenure",  hist=True, kde=False)
ax.fig.suptitle('Tenure distribution in customer churn', y=1, fontsize=16, fontweight='bold');
plt.legend();

Image generated by the code


Tags: size绘制axhue计数百分比snschurn
3条回答

您可以使用norm_hist = True

documentation开始:

norm_hist : bool, optional

If True, the histogram height shows a density rather than a count. This is implied if a KDE or fitted density is plotted.

  • 截至^{}
    • ^{}替换为图形级别^{}和轴级别^{},它们具有stat参数。使用stat='percent'
  • 对于这两种类型的图,使用common_binscommon_norm进行试验。
    • 例如,common_norm=True将显示占整个人口的百分比,而False将显示相对于群体的百分比
  • answer中所示的实现展示了如何添加注释
import seaborn as sns
import matplotlib.pyplot as ply

# data
data = sns.load_dataset('titanic')

图形级别

p = sns.displot(data=data, x='age', stat='percent', hue='sex', height=3)
plt.show()

enter image description here

p = sns.displot(data=data, x='age', stat='percent', col='sex', height=3)
plt.show()

enter image description here

  • labels中使用的类型批注(:=)需要python >= 3.8。这可以通过for-loop实现,而无需使用:=
fg = sns.displot(data=data, x='age', stat='percent', col='sex', height=3.5, aspect=1.25)

for ax in fg.axes.ravel():
    
    # add annotations
    for c in ax.containers:

        # custom label calculates percent and add an empty string so 0 value bars don't have a number
        labels = [f'{w:0.1f}%' if (w := v.get_height()) > 0 else '' for v in c]

        ax.bar_label(c, labels=labels, label_type='edge', fontsize=8, rotation=90, padding=2)
    
    ax.margins(y=0.2)

plt.show()

enter image description here

轴水平

fig = plt.figure(figsize=(4, 3))
p = sns.histplot(data=data, x='age', stat='percent', hue='sex')
plt.show()

enter image description here

您可以选择一个条形图,并设置一个估计器,以百分比定义标准化:

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

相关问题 更多 >