python绘图库seaborn_Matplotlib Toolkits:python高级绘图库seaborn

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的优点:

默认情况下就能创建赏心悦目的图表。(只有一点,默认不是jet colormap)

创建具有统计意义的图

能理解pandas的DataFrame类型,所以它们一起可以很好地工作。

安装pip install seaborn

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

importseaborn

importmatplotlib.pyplot asplt

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

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'

)

;

14004942_cYjf.jpg

如你所见,仅通过一行代码,我们就创建了一个漂亮复杂的统计图,其中包含拥有置信区间的最拟合回归直线、边界图,以及相关系数。

使用matplotlib重新绘制这幅图的话需要相当多的(丑陋)代码,包括调用scipy执行线性回归并手动利用线性回归方程绘制直线(我甚至想不出怎么在边界绘图,怎么计算置信区间)。

[

the tutorial on quantitative linear models]

与Pandas的DataFrame很好地工作

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

tips

.

head

(

)

Out[9]:

total_bill

tip

sex

smoker

day

time

size

0

16.99

1.01

Female

No

Sun

Dinner

2

1

10.34

1.66

Male

No

Sun

Dinner

3

2

21.01

3.50

Male

No

Sun

Dinner

3

3

23.68

3.31

Male

No

Sun

Dinner

2

4

24.59

3.61

Female

No

Sun

Dinner

4

我们可能想知道吸烟者给的小费是否与不吸烟的人不同。没有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"

)

;

14004942_HJ7N.jpg

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

http://blog.csdn.net/pipisorry/article/details/49515745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值