为了账号安全,请及时绑定邮箱和手机立即绑定

从计数图创建单独的 distplot

从计数图创建单独的 distplot

蝴蝶刀刀 2023-02-07 17:24:50
如何从 countplot 创建 distplotplt.rcdefaults()%config InlineBackend.figure_format='retina'sns.set_style('darkgrid')ax = sns.countplot(x='Age',hue='Gender',data=df,edgecolor="None")ax.tick_params(bottom=False, left=False)ax.set_axisbelow(True)for rect in ax.patches:        x = rect.get_x() + rect.get_width()/2.        y = rect.get_height()        try:            ax.annotate("{}".format(int(y)), (x,y), ha='center', va='bottom', clip_on=True)        except:            passax.set_xlabel('Age', color='green')ax.set_ylabel('Count', color='green')ax.set_title('Countplot for Age(Gender)', color='tomato',weight='bold')plt.legend(title='Gender', fontsize='large', loc='upper right').get_frame().set_facecolor('white')plt.tight_layout()plt.savefig('files\\Countplot_for_Age(Gender).jpg')
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

a 的 x 轴countplot是分类的:它为每个遇到的年龄放置一个条,当没有特定年龄的行时跳过条(示例中为 21 和 23)。在内部,条形编号为 0、1、2、...。y 轴是计数,与行数成正比。


对于 a distplot,x 轴是年龄本身,y 轴是概率分布,通常是非常小的数字(曲线下面积标准化为 1)。


因此,由于 x 轴和 y 轴不同,最好使用单独的子图。


Adistplot可以直接从给定的数据生成。ax在同一子图中的两个distplots 中传递相同的结果。Adistplot是直方图和 a 的组合kdeplot。如果不需要直方图, 可以hist=False省略

,或者kdeplot直接调用。该shade=True选项向绘图添加阴影。


from matplotlib import pyplot as plt

import seaborn as sns

import pandas as pd

import numpy as np


NF = 50

NM = 10

df = pd.DataFrame({'Age': np.concatenate([np.random.randint(13, 20, NF) + np.random.randint(2, 7, NF),

                                          np.random.randint(15, 23, NM)]),

                   'Gender': np.repeat(['female', 'male'], (NF, NM))})

df['Age'] = df['Age'].where((df['Age'] != 21) & (df['Age'] != 23), 20)


sns.set_style('darkgrid')


fig, axs = plt.subplots(ncols=2, figsize=(12, 4))


ax = sns.countplot(x='Age', hue='Gender', data=df, edgecolor="None", ax=axs[0])

ax.tick_params(bottom=False, left=False)

ax.set_axisbelow(True)


for rect in ax.patches:

    x = rect.get_x() + rect.get_width() / 2.

    y = rect.get_height()

    ax.annotate(f"{y:.0f}", (x, y), ha='center', va='bottom', clip_on=True)


ax.set_xlabel('Age', color='green')

ax.set_ylabel('Count', color='green')

ax.set_title('Countplot for Age(Gender)', color='tomato', weight='bold')

ax.legend(title='Gender', fontsize='large', loc='upper right').get_frame().set_facecolor('white')


for gender in ('female', 'male'):

    # ax2 = sns.kdeplot(df[df['Gender'] == gender]['Age'], shade=True, ax=axs[1], label=gender)

    ax2 = sns.distplot(df[df['Gender'] == gender]['Age'], hist=False, kde_kws={'shade': True}, ax=axs[1], label=gender)


ax2.set_axisbelow(True)

ax2.set_xlabel('Age', color='green')

ax2.set_ylabel('probability distribution', color='green')

ax2.set_title('Distplot for Age(Gender)', color='tomato', weight='bold')

ax2.legend(title='Gender', fontsize='large', loc='upper right').get_frame().set_facecolor('white')


plt.tight_layout()

plt.show()

//img1.sycdn.imooc.com//63e2191e00018da725600822.jpg

查看完整回答
反对 回复 2023-02-07
  • 1 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号