Python可视化—pandas、matplotlib、seaborn

1. pandas

1.1 导入相关库

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False  # 步骤二(解决坐标轴负数的负号显示问题)

1.2 读取数据

df = pd.read_csv('E:\学习\DataAnalyst-gbk1.csv',encoding='gbk')

在这里插入图片描述

1.3 添加一列数据

df['avg'] = (df.top+df.bottom)/2

在这里插入图片描述

1.4 绘制折线图

df.avg.plot(figsize=(100,6))

在这里插入图片描述
曲线是根据companyId绘制的,但由于公司较多,曲线看不出什么信息。所以我们根据平均工资的不同绘制不同平均工资的公司有多少。

df.avg.value_counts().sort_index().plot()

df.avg.value_counts()得到的是不同平均工资的公司的数量,索引是平均工资,这样直接绘制得到的图没有规律,所以我们要先将平均工资进行排序,然后再绘制。
在这里插入图片描述
可以看到大部分的工资都集中在9-30k之间。

1.5 柱形图

df.avg.value_counts().sort_index().plot(kind='bar')  
#df.avg.value_counts().sort_index().plot.bar()

在这里插入图片描述
绘制结果比较密集
可使用透视表进行绘制,透视表就可从不同维度进行划分:

df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot(kind='bar',stacked=True)

在这里插入图片描述
绘制不同城市里对学历的招聘要求的公司数量。可以看到背景的公司最多,其次是上海;招聘要求本科占绝大部分比率。

df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot(kind='barh',stacked=True)

kind = ‘barh’,图像方向改变。
在这里插入图片描述

1.6 直方图

针对1.5的柱形图数量太多的问题,可使用直方图划分区间来绘制。

df.avg.value_counts().sort_index().plot.hist(bins=20)

在这里插入图片描述

df.groupby('education').apply(lambda x:x.avg).unstack().T  # 反堆叠
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.hist(alpha=0.5,stacked=True,bins=20)

alpha是透明度。
在这里插入图片描述

1.7 箱线图

df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.box()

在这里插入图片描述

df.boxplot(column='avg',by='education')

在这里插入图片描述

1.8 密度图

df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.kde()

在这里插入图片描述

df.avg.plot.kde()

在这里插入图片描述

1.9 面积图

df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.area()

在这里插入图片描述

df.pivot_table(index='avg',columns='education',aggfunc='count',values='companyId').plot.area()

在这里插入图片描述

1.10 散点图

df.groupby('companyId').aggregate(['mean','count','max']).avg.plot.scatter(x='mean',y='count')

在这里插入图片描述

matrix = df.groupby('companyId').aggregate(['mean','count','max']).avg
pd.plotting.scatter_matrix(matrix)

在这里插入图片描述

1.11 饼图

df.city.value_counts().plot.pie(figsize=(6,<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值