Matplotlib Toolkits:python高级绘图库seaborn

Seaborn是一款基于Matplotlib的高级数据可视化库,用于绘制统计图表。它简化了复杂数据集的可视化过程,提供了美观的默认样式,并且与Pandas紧密集成。本文介绍了Seaborn的基本使用方法,包括如何安装、配置样式以及绘制各种统计图表。

http://blog.youkuaiyun.com/pipisorry/article/details/49515745

Seaborn介绍

seaborn

(Not distributed with matplotlib)

seaborn is a highlevel interface for drawing statistical graphics with matplotlib. Itaims to make visualization a central part of exploring andunderstanding complex datasets.

[seaborn ]

Matplotlib是Python主要的绘图库。但是不建议你直接使用它,原因与不推荐你使用NumPy是一样的。虽然Matplotlib很强大,它本身就很复杂,你的图经过大量的调整才能变精致。因此,作为替代推荐一开始使用Seaborn。

Seaborn本质上使用Matplotlib作为核心库(就像Pandas对NumPy一样)。

seaborn的优点:

  1. 默认情况下就能创建赏心悦目的图表。(只有一点,默认不是jet colormap
  2. 创建具有统计意义的图
  3. 能理解pandas的DataFrame类型,所以它们一起可以很好地工作。

安装pip install seaborn

Note: lz发现,就算你不用seaborn绘图,只要在matplotlib绘图中加上seaborn的import语句,就会以seaborn的图形方式展示图片,具有seaborn的效果。如:

import seaborn
import matplotlib.pyplot as plt

注意要显示出图形,需要引入matplotlib并plt.show()出来。

皮皮blog


Seaborn使用

Style functions: API | Tutorial

Color palettes: API | Tutorial

Distribution plots: API | Tutorial

Regression plots: API | Tutorial

Categorical plots: API | Tutorial

Axis grid objects: API | Tutorial

分布图绘制Distribution plots

jointplot(x, y[, data, kind, stat_func, ...])Draw a plot of two variables with bivariate and univariate graphs.
pairplot(data[, hue, hue_order, palette, ...])Plot pairwise relationships in a dataset.
distplot(a[, bins, hist, kde, rug, fit, ...])Flexibly plot a univariate distribution of observations.
kdeplot(data[, data2, shade, vertical, ...])Fit and plot a univariate or bivariate kernel density estimate.
rugplot(a[, height, axis, ax])Plot datapoints in an array as sticks on an axis.

单变量绘制distplot

seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)

Note:

1 如果想显示统计个数而不是概率,需要同时设置norm_hist=False, kde=False。

2 自己指定fit的函数from scipy import stats ... fit=stats.norm

>>> import seaborn as sns, numpy as np
>>> sns.set(rc={"figure.figsize": (8, 4)}); np.random.seed(0)
>>> x = np.random.randn(100)
>>> ax = sns.distplot(x)

双变量+单变量统一绘制jointplot

import seaborn as sns
 
# Load one of the data sets that come with seaborn
tips = sns . load_dataset ( "tips" )
 
sns . jointplot ( "total_bill" , "tip" , tips , kind = 'reg' ) ;
 

如你所见,仅通过一行代码,我们就创建了一个漂亮复杂的统计图,其中包含拥有置信区间的最拟合回归直线、边界图,以及相关系数。
使用matplotlib重新绘制这幅图的话需要相当多的(丑陋)代码,包括调用scipy执行线性回归并手动利用线性回归方程绘制直线(我甚至想不出怎么在边界绘图,怎么计算置信区间)。
[ the tutorial on quantitative linear models]

与Pandas的DataFrame很好地工作

数据有自己的结构。通常我们感兴趣的包含不同的组或类(这种情况下使用pandas中groupby的功能会让人感到很神奇)。比如tips(小费)的数据集是这样的:

tips . head ( )

Out[9]:

 total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4

我们可能想知道吸烟者给的小费是否与不吸烟的人不同。没有seaborn的话,这需要使用pandas的groupby功能,并通过复杂的代码绘制线性回归直线。使用seaborn的话,我们可以给col参数提供列名,按我们的需要划分数据:

回归图绘制Regression plots

lmplot(x, y, data[, hue, col, row, palette, ...])Plot data and regression model fits across a FacetGrid.
regplot(x, y[, data, x_estimator, x_bins, ...])Plot data and a linear regression model fit.
residplot(x, y[, data, lowess, x_partial, ...])Plot the residuals of a linear regression.
interactplot(x1, x2, y[, data, filled, ...])Visualize a continuous two-way interaction with a contour plot.
coefplot(formula, data[, groupby, ...])Plot the coefficients from a linear model.

sns . lmplot ( "total_bill" , "tip" , tips , col = "smoker" ) ;
 

很整洁吧?随着你研究得越深,你可能想更细粒度地控制这些图表的细节。因为seaborn只是调用了matplotlib,那时你可能会想学习这个库。

from: http://blog.youkuaiyun.com/pipisorry/article/details/49515745

ref: [seaborn API reference]*

[Seaborn tutorial]*

Python和数据科学的起步指南

Example gallery

Python数据可视化模块:Seaborn


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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值