从计数图创建单独的 distplot

如何从 countplot 创建 distplot


plt.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:

            pass


ax.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')

http://img4.mukewang.com/63e2190100016fa906300470.jpg

蝴蝶刀刀
浏览 148回答 1
1回答

慕的地6264312

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 pltimport seaborn as snsimport pandas as pdimport numpy as npNF = 50NM = 10df = 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python