#!/usr/bin/env python # coding: utf-8 # #
Python统计可视化库Seaborn
#
易红发 yihongfa@yeah.net
#   大家都知道Python中有一个强大的可视化库matplotlib,其中matplotlib.pyplot也是针对统计可视化,可问题在于matplotlib太过繁复,这里推荐另一个统计可视化库Seaborn,只需简单的几行代码,就可以画出相当漂亮的统计图。 #   另外,如果需要制作交互式图表,Python中我推荐[bokeh库](http://nbviewer.jupyter.org/github/bokeh/bokeh-notebooks/blob/master/quickstart/quickstart.ipynb) #   注:本分享可直接下载,然后用IPython notebook(现在叫jupyter)运行。 # #可视化连续数据集 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: import numpy as np import pandas as pd from scipy import stats, integrate import matplotlib.pyplot as plt # In[3]: import seaborn as sns sns.set(color_codes=True) # In[4]: np.random.seed(sum(map(ord, "distributions"))) # ## # In[8]: x = np.random.normal(size=100) sns.distplot(x)#默认增加趋势线 # In[9]: sns.distplot(x, kde=False, rug=True)#删除趋势线,增加地毯图 # In[10]: sns.distplot(x, bins=20, kde=False, rug=True)#将箱数设置为20 # In[11]: sns.distplot(x, hist=False, rug=True)#要趋势线,不要直方图 # In[13]: sns.kdeplot(x, shade=True)#也可以直接使用线图 # 一个新的参数 bw(banwidth)类似于直方图里面的箱数 # In[15]: sns.kdeplot(x) sns.kdeplot(x, bw=.2, label="bw:0.2") sns.kdeplot(x, bw=2, label="bw:2") plt.legend() # In[20]: sns.kdeplot(x, shade=True, cut=0)#删除极端值 sns.rugplot(x) # ##