python作图已成为数据分析中不可或缺的手段,常使用的包为matplotlib,它被很多其它包所依赖,例如pandas中具有的绘图功能就是对matplotlib的API的二次封装使用,今天要说明的seaborn包则提供了一种新的绘图功能,其简单易行的封装功能带来更为强大和美观的绘图效果,现简要说明该包[功能],主要分为三大类:(http://web.stanford.edu/~mwaskom/software/seaborn/tutorial.html)
风格管理
该部分提供的是关于绘图板风格及调色板颜色的设置,用户可以自行控制绘图界面。
- Controlling figure aesthetics(界面风格)
Styling figures with axes_style() and set_style()
Removing spines with despine()
Temporarily setting figure style
Overriding elements of the seaborn styles
Scaling plot elements with plotting_context() and set_context()
- Choosing color palettes(调色板)
Building color palettes with color_palette()
Qualitative color palettes
Sequential color palettes
Diverging color palettes
Changing default palettes with set_palette()
绘图函数
该部分主要提供三种方向使用的功能函数,分别是用于做分布图,线性图及分类图的,其中分类图中提供了小提琴等新颖的可视化方法。
- Visualizing the distribution of a dataset
Plotting univariate distributions
Plotting bivariate distributions
Visualizing pairwise relationships in a dataset
- Visualizing linear relationships
Functions to draw linear regression models
Fitting different kinds of models
Conditioning on other variables
Controlling the size and shape of the plot
Plotting a regression in other contexts
- Plotting with categorical data
Distributions of observations within categories
Statistical estimation within categories
Drawing multi-panel categorical plots
其中在对DataFrame支持的散点作图中,提供了pariplot()方法,直接实现两两变量间的散点分布图,也可利用g=PairGrid(data)实现对该种网格的定制化输出。
网格绘图
该部分实质上跟前面部分有重合,但其中有个FacetGrid的绘图方法,其余PairGrid有类似之处。
- Plotting on data-aware grids
Subsetting data with FacetGrid
Plotting pairwise relationships with PairGrid and pairplot()
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip", alpha=.7)
g.add_legend();
ps:还有kdeplot(),heatmap(),clustermap(),jointplot()等函数高效实现绘图功能等