python 绘制和密度图笔记

import pandasas pd

import numpyas np

import seabornas sns

import matplotlib.pyplotas plt

pd.set_option('display.max_columns', 10000)

pd.set_option('display.max_rows', 10000000000)

pd.set_option('display.width', 100000)

income = pd.read_excel(r'D:\bigData\0629demo\dataSource\income.xlsx')

fill_data = income.fillna(value={'workclass': income.workclass.mode()[0], 'occupation': income.occupation.mode()[0],

                                'native-country': income['native-country'].mode()[0]}, inplace=True)

# print(income.apply(lambda x: np.sum(x.isnull())))

# print(income)

print(income.describe())

print(income.describe(include=['object']))

# 设置绘图风格

plt.style.use('ggplot')

# 设置多图形组合

fig, axes = plt.subplots(2, 1)

# 绘制不同收入水平下的年龄核密度图

# kind='kde', label='<=50K', ax=axes[0], legend=True, linestyle='-'

# kind='kde', label='>50K', ax=axes[0], legend=True, linestyle='--'

income['age'][income.income ==' <=50K'].plot(kind='kde', ax=axes[0], label='<=50K', legend=True, linestyle='-')

income['age'][income.income ==' >50K'].plot(kind='kde', ax=axes[0], label='>50K', legend=True, linestyle='--')

# 绘制不同收入水平下的周工作小时数核密度图

# kind='kde', label='<= 50K', ax=axes[1], legend=True,  linestyle='-'

# kind='kde', label='> 50K', ax=axes[1], legend=True, linestyle='--'

income['hours-per-week'][income.income ==' <=50K'].plot(kind='kde', label='<= 50K', ax=axes[1], legend=True,

                                                        linestyle='-')

income['hours-per-week'][income.income ==' >50K'].plot(kind='kde', label='> 50K', ax=axes[1], legend=True,

                                                        linestyle='--')

plt.show()

# 构造不同收入水平下各种族人数的数据

race = pd.DataFrame(income.groupby(by=['race', 'income']).agg(np.size).loc[:, 'age'])

# 重设行索引

race = race.reset_index()

# 变量重命名

race.rename(columns={'age':'counts'}, inplace=True)

print(race)

# 排序

race.sort_values(by=['race', 'counts'], ascending=False, inplace=True)

# 构造不同收入水平下各家庭关系人数的数据

relationship = pd.DataFrame(income.groupby(by=['relationship', 'income']).agg(np.size).loc[:, 'age'])

relationship = relationship.reset_index()

relationship.rename(columns={'age':'counts'}, inplace=True)

relationship.sort_values(by=['relationship', 'counts'], ascending=False, inplace=True)

plt.figure(figsize=(15, 10))

sns.barplot(x='race', y='counts', hue='income', data=race)

plt.show()

plt.figure(figsize=(15, 10))

sns.barplot(x='relationship', y='counts', hue='income', data=relationship)

plt.show()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python Data Anlysis NotebookSublimeText FileData FrameIte...
    生煎小包阅读 759评论 0 1
  • Python Data Science Handbook About Archive [图片上传中...(imag...
    榴莲气象阅读 608评论 0 1
  • # -*- coding: utf-8 -*- from __future__ import division f...
    小豆角lch阅读 1,473评论 0 1
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,594评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,224评论 4 8