首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用seaborn distplot / histplot / displot绘制百分比

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

Stack Overflow用户
提问于 2020-08-12 16:59:44
回答 3查看 5.2K关注 0票数 2

有没有一种方法可以在distplot上绘制百分比而不是计数?

代码语言:javascript
运行
复制
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();

EN

回答 3

Stack Overflow用户

发布于 2021-08-19 15:44:30

  • 对于这两种类型的绘图,请尝试使用common_binscommon_norm
    • 例如,common_norm=True将显示百分比作为整个总体的一部分,而False将显示相对于group.

的百分比

  • answer中显示的实现说明了如何添加annotations.

代码语言:javascript
运行
复制
import seaborn as sns
import matplotlib.pyplot as ply

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

图级别

代码语言:javascript
运行
复制
p = sns.displot(data=data, x='age', stat='percent', hue='sex', height=3)
plt.show()

代码语言:javascript
运行
复制
p = sns.displot(data=data, x='age', stat='percent', col='sex', height=3)
plt.show()

labels中使用的

  • 类型批注(:=)需要python >= 3.8。这可以在不使用:=.

的情况下使用for-loop来实现

代码语言:javascript
运行
复制
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()

轴标高

代码语言:javascript
运行
复制
fig = plt.figure(figsize=(4, 3))
p = sns.histplot(data=data, x='age', stat='percent', hue='sex')
plt.show()

按组列出的百分比

使用parameter

  • common_norm=

代码语言:javascript
运行
复制
p = sns.displot(data=data, x='age', stat='percent', hue='sex', height=4, common_norm=False)

代码语言:javascript
运行
复制
p = sns.displot(data=data, x='age', stat='percent', col='sex', height=4, common_norm=False)

代码语言:javascript
运行
复制
fig = plt.figure(figsize=(5, 4))
p = sns.histplot(data=data, x='age', stat='percent', hue='sex', common_norm=False)
plt.show()

票数 6
EN

Stack Overflow用户

发布于 2020-08-12 17:12:15

您可以使用norm_hist = True

documentation

norm_hist:布尔型,可选

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

票数 0
EN

Stack Overflow用户

发布于 2020-08-12 17:16:18

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

代码语言:javascript
运行
复制
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()

给予:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63373194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档