Seaborn教程

原教程

绘图风格管理

控制图形审美

matplotlib是高度自定义的,但是很难去知道怎么调节参数获得一个很漂亮的plot,Seaborn库包含一些定制好的主题和一些高层次的参数去调节matplotlib的图像。

Seaborn库把matplotlib的参数分为两种,一种是设置图形的审美外观风格,另一种则是为了调节缩放以达到更好的观察效果。
设置风格通过axes_style()和set_style()实现,为了缩放图形一般用plotting_context()和set_context();对于两种情况,前一个函数类似于get,后一个类似于set。

风格

Seaborn内置5种主题:darkgrid, whitegrid, dark, white和ticks,默认为darkgrid

sns.set_style('whitegrid')
data = np.random.normal(size=(20,6))+np.arange(6)/2
sns.boxplot(data=data)
缩放

缩放在seaborn库内相关的尺寸有:paper, notebook, talk和poster, notebook是默认值。

sns.set_context('paper')
plt.figure(figsize=(8,6))
sns.boxplot(data)
选择颜色方案

选择颜色方案是一个很有技巧的事情,Seaborn库定制一些颜色方案可使用。
一般使用到color_palette()和set_palette(),三种类型的调色板:qualitative, sequential和diverging
1. Qualitative 用于离散化没有固定顺序的分块数据

current_palette = sns.color_palette()
sns.palplot(current_palette) #plot current color palette;
sns.set_palette("husl")
pal = sns.dark_palette('purple', as_cmap=True)
sns.kdeplot(x, y, cmap=pal)
  1. Sequential 用于有顺序的分块数据
  2. diverging 用于中间有分割点的分块数据

绘图函数

数据分布可视化
  1. 单变量分布
    sns.distplot默认画出一个直方图和kde(核密度估计)图
x = np.random.normal(size=100)
sns.distplot(x, kde=True , rug=True, hist=True)
#sns.rugplot
#sns.kdeplot

核密度估计是非参数估计,关于核密度估计我会出一个详细的博客,distplot也可以采用拟合参数估计。

x = np.random.gamma(6, size=200)
sns.distplot(x, kde=False, fit=stats.gamma)
  1. 双变量分布
    类似于二维的rug plot, kde plot, kind对应的有kde, hex
sns.jointplot(x = 'x', y='y', data =df, kind='kde')
  1. 成对特征之间的关系
sns.pairplot(iris)
g = sns.PairGrid(iris)
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.kdeplot, cmap='Blues_d', n_levels=6)
回归关系可视化

画出包含95%置信区间的线性回归结果,线性的和多项式的拟合结果都可以,regplot()的参数可以调节。residplot可以画出拟合后的残差结果。

sns.regplot(x='total_bill', y='tip', data=tips)
sns.regplot(x='total_bill', y='tip', data=tips)
sns.residplot() ### plot residual value to check model
sns.jointplot(kind='reg')
类别型变量可视化
  1. 散点图
sns.stripplot(x='day', y='total_bill',data=tips, jitter=True);
sns.swarmplot(x, y, hue) # can add another categorical variable with hue parameter
  1. 变量的分布图
sns.boxplot(x,y,hue)
sns.violinplot(x,y,hue)  #boxplot with kde
  1. 统计估计
sns.barplot(x,y,hue)
sns.countplot(x, data)
sns.pointplot(x, y,hue)
  1. 绘制多面板的相关图
    类似于更高层次的pairplot
sns.factorplot(x, y, hue, col, kind)
数据敏感的gridplot
g = sns.FacetGrid(tips, col='time',row='age', hue='smoker')
g.map(plt.hist, 'total_bill', 'tip') #plt.hist can be a customized plot function
参考文献:
  1. 用Seaborn画出好看的分布图
  2. seaborn tutorial
1. 目录 1. 目录 2 2. 绘图函数Plotting functions 4 2.1. 可视化的统计关系Visualizing statistical relationships 4 2.1.1. 用散点图联系变量Relating variables with scatter plots 4 2.1.2. 强调线条图的连续性Emphasizing continuity with line plots 10 2.1.3. 显示与切面的多个关系Showing multiple relationships with facets 21 2.2. 分类数据绘图Plotting with categorical data 24 2.2.1. 分类散点图Categorical scatterplots 26 2.2.2. 分类观测值分布Distributions of observations within categories 31 2.2.3. 分类统计估计Statistical estimation within categories 37 2.2.4. 对“wide-form”数据作图Plotting “wide-form” data 41 2.2.5. 显示与facet的多个关系Showing multiple relationships with facets 43 2.3. 可视化数据集的分布Visualizing the distribution of a dataset 44 2.3.1. 绘制单变量分布Plotting univariate distributions 45 2.3.2. 绘制二元分布Plotting bivariate distributions 51 2.3.3. 在数据集中可视化成对关系Visualizing pairwise relationships in a dataset 55 2.4. 可视化线性关系Visualizing linear relationships 57 2.4.1. 函数绘制线性模型Functions to draw linear regression models 58 2.4.2. 拟合不同种类的模型Fitting different kinds of models 61 2.4.3. 在其他变量上的情况Conditioning on other variables 68 2.4.4. 控制图表的大小和形状Controlling the size and shape of the plot 71 2.4.5. 在其他上下文中绘制回归图Plotting a regression in other contexts 73 3. 多图网格Multi-plot grids 76 3.1. 构建结构化的多图网格Building structured multi-plot grids 76 3.2. 有条件的小倍数Conditional small multiples 77 3.3. 使用定制函数Using custom functions 86 3.4. 绘制成对的数据关系Plotting pairwise data relationships 90 4. 绘图美学Plot aesthetics 99 4.1. 控制图表美学Controlling figure aesthetics 99 4.1.1. Seaborn图表风格Seaborn figure styles 101 4.1.2. 删除轴上的小凸起Removing axes spines 104 4.1.3. 临时设置图表样式Temporarily setting figure style 105 4.1.4. 覆盖Seaborn样式的元素Overriding elements of the seaborn styles 106 4.1.5. 缩放图表元素Scaling plot elements 108 4.2. 选择调色板Choosing color palettes 111 4.2.1. 创建颜色调色板Building color palettes 111 4.2.2. 定性调色板Qualitative color palettes 112 4.2.3. 连续调色板Sequential color palettes 116 4.2.4. 不同颜色的调色板Diverging color palettes 122 4.2.5. 设置默认调色板Setting the default color palette 124 5. 教程中的数据集 125
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值