秦路《七周》Python部分 -- 可视化笔记(pandas&matplotlib)

# 导入第三方包
import pandas as pd
%matplotlib inline
df = pd.read_csv('position_gbk.csv',encoding = 'gbk')
  • 折线图
  • 条形图
  • 直方图
  • 箱线图
  • 密度图
  • 密度图
  • 面积图
  • 散点图
  • 散点图矩阵
  • 饼图
df.avg.value_counts().sort_index().plot()  # 折线图  
折线图
df.avg.value_counts().sort_index().plot.bar()  
# 条形图 (x轴的问题会用matplotlib包解决)
条形图
df.pivot_table(index='city',columns='education',values='avg',
aggfunc='count').plot.bar(stacked = True) 
# 堆积条形图(中文显示不出来的问题会用matplotlib包解决)
堆积条形图
df.avg.plot.hist() # 直方图
直方图
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.hist(alpha = 0.5,stacked = True) # 堆积直方图
堆积直方图
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.box() # 箱线图作图方法一
df.boxplot(column='avg',by='education') # 箱线图作图方法二
箱线图
df.avg.plot.kde() # 密度图
密度图
df.pivot_table(index='avg',columns='education',aggfunc='count',values='positionId').plot.area() # 面积图
面积图
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)  # 散点图矩阵
散点图矩阵
df.city.value_counts().plot.pie(figsize = (6,6))  # 饼图
饼图
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']  # 将字体更改为黑体,以解决中文识别问题
plt.rcParams['axes.unicode_minus'] = False # 用以显示负号
plt.pie(df.groupby('city').avg.count(),labels=df.groupby('city').avg.count().index)
plt.figure(1,figsize=(12,4))
plt.plot(np.random.random_integers(-20,20,20),label = 'nol')
plt.title('这是一个折线图')
plt.xticks([0,20,30])
plt.plot(np.random.random_integers(-20,20,20),label = 'no2')
plt.legend()
plt.show()
这是一个折线图
data = df.groupby(['education','companyId']).aggregate(['mean','count']).avg.reset_index()
for edu,grouped in data.groupby('education'):
    x=grouped['mean']
    y=grouped['count']
    plt.scatter(x,y,label=edu)
plt.legend()
散点图
plt.figure(figsize=(12,4))
plt.subplot(2,2,1)
plt.plot(np.random.random_integers(-20,20,20),label='no1')
plt.subplot(2,2,2)
plt.plot(np.random.random_integers(-20,20,20),label='no2')
plt.subplot(2,1,2)
plt.plot(np.random.random_integers(-20,20,20),label='no1')
子图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容