看了一篇比较matplotlib\pandas\seaborn\ggplt\Altair的很好的文章,分享给大家。另外,从中总结一下使用最多的Seaborn的用法
- 场景一:在一张图上画多个时间序列
g=sns.FacetGrid(ts,hue='kind',size=5,aspect=1.5)
g.map(plt.plot,'dt','value').add_legend()
g.ax.set(xlabel='Date',ylabel='Value',title='Random Timeseries')
g.fig.autofmt_xdate()
解读:给FacetGrid传入原始数据,将kind赋值给hue参数的意思是绘制四条不同的线,每条线对应一种kind。而真正要画出这四条线,要把FacetGrid映射(map)到matplotlib的plot函数中,并传入x轴和y轴参数,如第二行代码所示。当然,这里面不是单纯的图形映射,而是函数映射:数据集中每一个hue都会调用matplotlib的plot函数,循环是不可见的底层实现。
- 场景二:如何画散点图?
g=sns.FacetGrid(df,hue='species',size=7.5)
g.map(plt.scatter,'petalLength','petalWidth').add_legend()
g.ax.set_title('Petal Width v. Length --by Species')
解读:对比场景一,这里同样构建FacetGrid,只是将plt.plot换成plt.scatter
- 场景三:如何画分开的散点图?
g=sns.FacetGrid(df,col='species',hue='species',size=5)
g.map(plt.scatter,'petalLength','petalWidth')
解读:增加col变量,这会告诉FacetGrid不仅给每个种类一个唯一的颜色,还把每个种类都画在唯一的子图上,按列排列。(只要将col变量换成row就可以按行排列。)
g = sns.FacetGrid(df, col='species', hue='species', size=5)
g.map(plt.scatter, 'petalLength', 'petalWidth')
- 场景四:怎么可视化分布?
1、箱形图
fig,ax=plt.subplots(1,1,figsize=(10,10))
g=sns.boxplot('species','petalWidth',data=df,ax=ax)
g.set(title='Distribution of Petal Width by Species')
2、条形图
g=sns.FacetGrid(df,hue='species',size=7.5)
g.map(sns.distplot,'petalWidth',bins=10,kde=False,rug=True).add_legend()
g.set(xlabel='Petal Width',ylabel='Frequency',title='Distribution of Petal Width by Species')
解读:条形图中可以加核密度估计(KDEs)和轴须图(rugplots)
3、用factorplot画分类图
The default plot that is shown is a point plot, but other seaborn categorical plots can be chosen with the kind parameter, including box plots, violin plots, bar plots, or strip plots.
g=sns.factorplot(x='class',y='fare',hue='survived',data=df,kind='bar',order=['First','Second','Third'],size=7.5,aspect=1.5)
g.ax.set_title('Fare by survival and class')
解读:
a)对谁进行分组,class和survived,对应x和hue
b)对哪个数据列进行摘要统计,这里就是fare,对应到y变量
c)默认的摘要统计方法是求平均值(y轴),也可以通过factorplot提供的estimator参数进行修改,比如改为求和,标准差,中位数等等。
d)kind参数可以修改, {point, bar, count, box, violin, strip}
e)为了呈现统计的准确性,条形图上有误差线