一、样式与分布图
1.seaborn样式
seaborn中有预先设计好的5种主题,darkgrid、dark、whitegrid、white和ticks,默认使用darkgrid。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
years=[1950,1960,1970,1980,1990,2000,2010]
gdp=[300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
sns.set_style('darkgrid')
plt.scatter(years,gdp)

在seaborn中,set方法最常用,可以同时设置主题、调色板等多个样式。style为主题设置,palette用于设置调色板,color_codes设置颜色代码,设置后可以用r、g来设置颜色。
sns.set(style='white',palette='muted',color_codes=True)

2. 坐标轴移除
在seaborn中可以使用despine方法去除顶部和右侧坐标轴
sns.set(style='white',palette='muted',color_codes=True)
plt.plot(np.arange(10))
sns.despine()

设置offset参数可以偏移坐标轴,trim参数可以修剪刻度。
sns.set(style='white',palette='muted',color_codes=True)
plt.plot(np.arange(10))
sns.despine(offset=10,trim=True)

也可以指定移除哪些坐标轴
sns.set(style='whitegrid',palette='muted',color_codes=True)
plt.plot(np.arange(10))
sns.despine(left=True,bottom=True)

3. 单变量分布图
tips=sns.load_dataset('tips')
tips.head()

对于单变量分布图的绘制,在seaborn中使用distplot函数,默认情况下会绘制一个直方图,并嵌套一个与之对应的密度图。
sns.set(color_codes=True)
sns.distplot(tips['total_bill'])

在distplot中,可以通过kde参数选择不绘制密度图,使用rug参数可以绘制毛毯图。
sns.distplot(tips['total_bill'],kde=False,rug=True)

在displot中可以通过bins参数来设置分度。
sns.distplot(tips['total_bill'],bins=30,kde=False,rug=True)

在distplot中,可以设置hist为False来去掉直方图而绘制密度图。
sns.distplot(tips['total_bill'],hist=False,rug=True)

distplot可以同时绘制直方图、密度图和毛毯图,但这些分布图都有对应的具体绘图函数,其中kdeplot可以绘制密度图,rugplot可以绘制毛毯图。
fig,axes=plt.subplots(1,2,figsize=(10,5))
sns.distplot(tips['total_bill'],ax=axes[0],kde=False)
sns.rugplot(tips['total_bill'],ax=axes[0])
sns.kdeplot(tips['total_bill'],ax=axes[1],shade=True) #shade加阴影
sns.rugplot(tips['total_bill'],ax=axes[1])

4.多变量分布图
在matplotlib中,绘制两个变量的分布关系常用散点图。在seaborn中使用jointplot函数绘制一个多面板图,不仅可以显示两个变量的关系,也可以显示每个单变量的分布情况。
sns.jointplot(x='tip',y='total_bill',data=tips)
#x,y是列名
#data是数据来源

在jointplot函数中,改变kind参数为kde,单变量的分布就会用密度图来代替,而散点图会被等高线代替。
sns.jointplot(x='tip',y='total_bill',data=tips,kind='kde')

在数据集中,如果要体现多变量的分布情况,就要成对的二元分布图。在seaborn中,可以使用pairplot来完成二元分布图。该函数会创建一个轴矩阵,以此显示DataFrame中每两列的关系。
sns.pairplot(tips)

二、分类图
1. 散点分类图
在seaborn中,通过stripplot函数可以显示度量变量在每个类别的值。
sns.set(style='white',color_codes=True)
sns.stripplot(x='day',y='total_bill',data=tips)
sns.despine()

由于数据较多,很多散点会被覆盖,可以开抖动(jitter=True),这样可以看清更多数据点。
sns.set(style='white',color_codes=True)
sns.stripplot(x='day',y='total_bill',data=tips,jitter=True)
sns.despine()

如果要看清每一个数据点,可以用swarmplot函数。
sns.swarmplot(x='day',y='total_bill',data=tips)
sns.despine()

通过swarmplot函数的hue参数可以嵌套多一个分类变量,在图表中会以不同的色彩来表现。
sns.swarmplot(x='day',y='total_bill',hue='sex',data=tips)
sns.despine()

2. 箱线图与琴形图
在seaborn中用boxplot来绘制箱线图
sns.boxplot(x='day',y='total_bill',hue='time',data=tips)
sns.despine()

在seaborn中,使用violinplot来绘制琴形图。
sns.set(style='whitegrid',color_codes=True)
sns.violinplot(x='total_bill',y='day',hue='time',data=tips)

利用split参数可以将分类数据进行切分,这样绘制的琴形图两边的颜色就代表了不同的类别。利用inner参数可以对每个数据进行可视化。
fig,axes=plt.subplots(1,2,figsize=(10,5))
sns.violinplot(x='day',y='total_bill',hue='sex',data=tips,
split=True,ax=axes[0])
sns.violinplot(x='day',y='total_bill',hue='sex',data=tips,
split=True,inner='stick',palette='Set3',ax=axes[1])

这些分类图可以相互组合,实现更强大的可视化效果。
fig,axes=plt.subplots(1,2,figsize=(10,5))
sns.violinplot(x='day',y='total_bill',data=tips,
inner=None,ax=axes[0])
sns.swarmplot(x='day',y='total_bill',data=tips,
color='w',alpha=.5,ax=axes[0])
sns.boxplot(x='day',y='total_bill',data=tips,ax=axes[1])
sns.stripplot(x='day',y='total_bill',data=tips,
jitter=True,color='w',alpha=.5,ax=axes[1])

3.柱状图
在seaborn中使用barplot函数来绘制柱状图,默认情况下使用barplot函数绘制的y轴是平均值,且在每个柱状条上绘制误差条。
sns.barplot(x='sex',y='tip',hue='day',data=tips)

在柱状图中,常绘制类别的计数柱状图,如果使用matplotlib函数进行绘制,需要对DataFrame进行计算,而在seaborn中,使用countplot函数即可。
sns.countplot(x='size',data=tips,palette='Greens_d')

三、回归图与网格
1.回归图
在seaborn中可以,可以使用lmplot函数和regplot函数来绘制回归图,其绘制的图表是一样的。
sns.set(style='darkgrid',color_codes=True)
sns.lmplot(x='total_bill',y='tip',data=tips)

也可以不绘制置信区间
sns.set(style='darkgrid',color_codes=True)
sns.lmplot(x='total_bill',y='tip',data=tips)

可以使用hue参数加入一个分类的变量,通过不同颜色来表现
sns.lmplot(x='total_bill',y='tip',hue='smoker',data=tips)

如果再添加一个变量,可以绘制子图。
sns.lmplot(x='total_bill',y='tip',hue='smoker',col='time',data=tips)

若再添加两个变量。
sns.lmplot(x='total_bill',y='tip',hue='smoker',col='time',row='sex',data=tips)

2. 网格
对多维度数据进行可视化时,在数据集的不同子集上绘制同一个绘图的多个实例是一件非常有用的事情,这种技术被称为网格技术。在seaborn中,使用FacetGrid来创建对象,然后使用map就可以绘制多个实例图表了。
g=sns.FacetGrid(tips,col='time')
g.map(plt.hist,'tip')

也可以添加多个变量用于绘制多个图表。
g=sns.FacetGrid(tips,col='sex',row='time')
g.map(sns.boxplot,'size','tip')

matplotlib中的基本图形都可以在对象中绘制。
g=sns.FacetGrid(tips,col='sex',hue='smoker')
g.map(plt.scatter,'total_bill','tip',alpha=0.7)
g.add_legend()

网友评论