可视化部分的笔记可能没之前记的那么详尽,特别是seaborn这部分,特别是绘图函数的参数,我认为等以后用到这些图的时候再去研究详细的参数,记录笔记是要知道有这些图可以使用。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
分布
- distplot 概率分布图
- kdeplot 概率密度图
- jointplot 联合密度图
- painplot 多变量图
分类
- boxplot 箱线图
- violinplot 提琴图
- barplot 柱形图
- factorplot 因子图
线性
- lmplot 回归图
- heatmap 热力图
columns = ['use_id','order_dt','order_products','order_amount']
df = pd.read_table('CDNOW_master.txt',names=columns,sep='\s+')
sns.distplot(df.order_amount) #概率分布图

sns.kdeplot(df.order_amount) #概率密度图

grouped_user =df.groupby('use_id').sum()
sns.jointplot(grouped_user.order_products,grouped_user.order_amount) #联合密度图

df['order_dt'] =pd.to_datetime(df.order_dt,format = '%Y-%m-%d')
rfm = df.pivot_table(index = 'use_id',
values = ['order_products','order_amount','order_dt'],
aggfunc = {'order_products':'max',
'order_amount':'sum',
'order_dt':'count'
})
rfm['R']=-(rfm.order_dt - rfm.order_dt.max())
rfm.rename(columns={'order_products':'F','order_amount':'M'},inplace = True)
sns.jointplot(rfm.R,rfm.F) #联合密度图

sns.pairplot(rfm[['R','F','M']]) # 多变量图

plt.rcParams['font.sans-serif'] ='SimHei'
df = pd.read_csv('cy.csv',encoding='gbk')
plt.figure(figsize = (20,5))
sns.boxplot(x='类型',y='口味',data=df) # 箱线图

df2 =df.query("(城市 == '上海')|(城市 =='北京')")
plt.figure(figsize = (20,5))
sns.violinplot(x='类型',y='口味',hue='城市',data=df2,split =True)

plt.figure(figsize = (20,5))
sns.violinplot(x='类型',y='口味',hue='城市',data=df2,split =True) # 提琴图

sns.factorplot(x='类型',y='口味',data=df2,size=10) # 因子图

sns.lmplot(x='口味',y='环境',data=df) # 回归图

pt =df.pivot_table(index='城市',columns='类型',values='口味',aggfunc='mean')
plt.figure(figsize=(20,20))
sns.heatmap(pt) # 热力图

网友评论