#coding=utf-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
sns.set(style = "whitegrid",color_codes = True)
np.random.seed(sum(map(ord,"categorical")))
titanic = sns.load_dataset("titanic")#载入3个sns自带的数据集
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")
#多变量分析绘图
sns.stripplot(x = "day",y = "total_bill",data = tips,jitter = False)#输出此数据的"星期"项
plt.show()#星期只有7个值 大量数据会混在一起 互相遮挡 重叠
sns.stripplot(x = "day",y = "total_bill",data = tips,jitter = True)#数据左右偏移
plt.show()
sns.swarmplot(x = "day",y = "total_bill",data = tips)#也是偏移但美观些
plt.show()
sns.swarmplot(x = "day",y = "total_bill",hue = "sex",data = tips)#hue = 要统计的特征
plt.show()
sns.swarmplot(x = "total_bill",y = "day",hue = "sex",data = tips)#x、y互换 竖着画
plt.show()
#小提琴图
sns.violinplot(x = "day",y = "total_bill",hue = "sex",data = tips)
plt.show()
sns.violinplot(x = "day",y = "total_bill",hue = "sex",data = tips,split = True)
plt.show()
#合并 点图和小提琴图合并 两条语句构成此效果
sns.violinplot(x = "day",y = "total_bill",data = tips,inner = None)
sns.swarmplot(x = "day",y = "total_bill",data = tips,color = "w",alpha = 0.8)
plt.show()
#泰坦尼克数据按照不同性别在不同等级的船舱的获救概率
sns.barplot(x = "sex",y = "survived",hue = "class",data = titanic);
plt.show()
#同样的数据改用点图绘制 点图可以更好的描述变化差异
sns.pointplot(x = "sex",y = "survived",hue = "class",data = titanic);
plt.show()
#点图设置颜色使更美观
sns.pointplot(x = "class",y = "survived",hue = "sex",data = titanic,
palette = {"male":"g","female":"m"},
markers = ["^", "o"],
linestyles = ["-", "--"]);
plt.show()
#多层面板分类图 常用函数:sns.factorplot 此函数可画各种图 kind参数指定图形类型
seaborn.factorplot(x=None,
y=None,
hue=None,
data=None,
row=None,
col=None,
col_wrap=None,
estimator=,
ci=95,
n_boot=1000,
units=None,
order=None,
hue_order=None,
row_order=None,
col_order=None,
kind='point',
size=4,
aspect=1,
orient=None,
color=None,
palette=None,
legend=True,
legend_out=True,
sharex=True,
sharey=True,
margin_titles=False,
facet_kws=None,
**kwargs)
Parameters:
x,y,hue:数据集变量 变量名
date:数据集 数据集名
row,col:更多分类变量进行平铺显示 变量名
col_wrap:每行的最高平铺数 整数
estimator:在每个分类中进行矢量到标量的映射 矢量
ci:置信区间 浮点数或None
n_boot:计算置信区间时使用的引导迭代次数 整数
units:采样单元的标识符,用于执行多级引导和重复测量设计 数据变量或向量数据
order, hue_order:对应排序列表 字符串列表
row_order, col_order:对应排序列表 字符串列表
kind : 可选:point 默认, bar 柱形图, count 频次, box 箱体, violin 提琴, strip 散点,swarm 分散点
size:每个面的高度(英寸) 标量
aspect:纵横比 标量
orient:方向 "v"/"h"
color:颜色 matplotlib颜色
palette:调色板
seaborn:颜色色板或字典
legend:hue的信息面板 True/False
legend_out:是否扩展图形,并将信息框绘制在中心右边 True/False
share{x,y}:共享轴线 True/False
sns.factorplot(x = "day",y = "total_bill",hue = "smoker",data = tips)#默认画折线图
plt.show()
sns.factorplot(x = "day",y = "total_bill",hue = "smoker",data = tips,kind = "bar")#设置画条形图
plt.show()
sns.factorplot(x = "day",y = "total_bill",hue = "smoker",col = "time",data = tips,kind = "swarm")
plt.show()
#FacetGrid用做绘制数据集的子集 即用于绘制各变量之间的关系图
print(tips.head())
g = sns.FacetGrid(tips,col = "sex",hue = "smoker")
g.map(plt.scatter,"total_bill","tip",alpha = 0.7)
plt.show()