类别散点图
类别分布图
类别统计估计图
绘制宽格式的数据
x,y参数
设置图表的大小、坐标系
绘制分面类别图
PairGrid对象
Axis grids对象
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid", color_codes=True)
np.random.seed(sum(map(ord, "categorical")))
%matplotlib inline
It's useful to divide seaborn's categorical plots into three groups:
swarmplot()、stripplot()
boxplot()、violinplot()
barplot()、pointplot()
factorplot()和FacetGrid对象
titanic = sns.load_dataset("titanic")
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")
当一个变量是类别型变量,另一个变量是连续型变量
sns.stripplot(x="day", y="total_bill", data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x9cd4978>
sns.stripplot(x="day", y="total_bill", data=tips, jitter=True) # jitter=True,使散点在类别变量轴方向扰动,更容易看清连续型变量的分布,解决散点重叠问题
<matplotlib.axes._subplots.AxesSubplot at 0x9ec8470>
采用特定算法,将散点放置在类别变量轴上,更容易看清连续型变量的分布,解决散点重叠问题
sns.swarmplot(x="day", y="total_bill", data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0xa135da0>
sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips) # 将第3个类别型变量赋予hue参数
<matplotlib.axes._subplots.AxesSubplot at 0xab3c3c8>
在pandas中可以设置变量的顺序(Python数据分析一书中没有,Categorical数据类型是后来加入的)
sns.swarmplot(x="size", y="total_bill", data=tips) # 数值型类别变量,变量在图表中的位置,按数值大小排序
<matplotlib.axes._subplots.AxesSubplot at 0xad175f8>
sns.swarmplot(x="total_bill", y="day", hue="time", data=tips) # 将类别型变量赋予x,连续型变量赋予y,Seaborn可以自动推断出要绘制水平类别散点图
<matplotlib.axes._subplots.AxesSubplot at 0xafda780>
sns.swarmplot(x="total_bill", y="day", orient='h', hue="time", data=tips) # 当类别型变量是数值型变量时,需要显式设置orient='h'
<matplotlib.axes._subplots.AxesSubplot at 0xb1e5f98>
sns.boxplot(x="day", y="total_bill", hue="time", data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0xaf42eb8>
箱线图和核密度图的结合
sns.violinplot(x="total_bill", y="day", hue="time", data=tips)
sns.violinplot(x="total_bill", y="day", hue="time", data=tips,
bw=.1, scale="count", scale_hue=False) # 小提琴图涉及到核密度图,因而相比于箱线图,有更多参数可以设置
<matplotlib.axes._subplots.AxesSubplot at 0xbb82f98>
sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True) # 当hue变量为二元变量时,可以设置split参数
<matplotlib.axes._subplots.AxesSubplot at 0xbd557f0>
sns.violinplot(x="day", y="total_bill", hue="sex", data=tips,
split=True, inner="stick", palette="Set3") # inner参数控制如何小提琴的内部,inner="stick"为每个观察值绘制一条stick
<matplotlib.axes._subplots.AxesSubplot at 0xc0d37b8>
sns.violinplot(x="day", y="total_bill", data=tips, inner=None) # 注意设置inner=None,去除小提琴内部的box
sns.swarmplot(x="day", y="total_bill", data=tips, color="w", alpha=.5) # 注意设置alpha参数,调整散点透明度
<matplotlib.axes._subplots.AxesSubplot at 0xc9a1198>
# 计算每一类别中,连续型变量的均值,用bar的高度进行展示
# 通过自助法来计算每一个类别中连续型变量的置信区间,并用误差条进行展示
sns.barplot(x="sex", y="survived", hue="class", data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0xc9d3eb8>
简单的条形图(条的长度表示该类别中变量的个数)
sns.countplot(x="deck", data=titanic, palette="Greens_d")
<matplotlib.axes._subplots.AxesSubplot at 0xc9d6cc0>
sns.countplot(y="deck", hue="class", data=titanic, palette="Greens_d") # 设置hue参数,条件于第3个变量
<matplotlib.axes._subplots.AxesSubplot at 0xcd0ef98>
# 均值用点而非bar的高度展示
# 同样会绘制置信区间,用误差条展示
# 用直线连接同一个hue类别内的点,利于观察连续型变量均值随着类别型变量的变动而变动的趋势
sns.pointplot(x="sex", y="survived", hue="class", data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0xcf1dcf8>
sns.pointplot(x="class", y="survived", hue="sex", data=titanic,
palette={"male": "g", "female": "m"},
markers=["^", "o"], linestyles=["-", "--"]) # 设置palette、markers、linestyle参数
<matplotlib.axes._subplots.AxesSubplot at 0xd34a438>
iris.head()
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
sns.boxplot(data=iris) # 宽格式的数据(DataFrame、二维numpy数组等)可以直接传给data,用于绘制类别型数
<matplotlib.axes._subplots.AxesSubplot at 0xd656e48>
sns.boxplot(data=iris, orient="h")
<matplotlib.axes._subplots.AxesSubplot at 0xd9903c8>
sns.violinplot(x=iris.species, y=iris.sepal_length) # x,y参数不仅接受字符串形式的列名,还可以接受pandas或numpy对象
<matplotlib.axes._subplots.AxesSubplot at 0xdc7d208>
f, ax = plt.subplots(figsize=(7, 3)) # 通过matplotlib的figsize设置图表的大小,以及绘制的坐标系(可以将多个图表置于同一坐标系)
sns.countplot(y="deck", data=titanic, color="c")
<matplotlib.axes._subplots.AxesSubplot at 0xdd8fda0>
FacetGrid对象 + map方法、sns.factorplot() + kind参数,参见第4章“多个变量相对于一个变量的回归线”
sns.factorplot(x="day", y="total_bill", hue="smoker", data=tips) # 默认绘制sns.pointplot()
<seaborn.axisgrid.FacetGrid at 0xd6b63c8>
kind : {point, bar, count, box, violin, strip, swarm}
sns.factorplot(x="day", y="total_bill", hue="smoker", data=tips, kind="bar") # sns.barplot()
<seaborn.axisgrid.FacetGrid at 0xdeca630>
hue、col、row参数,参见第4章中“条件于其他变量”部分
sns.factorplot(x="day", y="total_bill", hue="smoker",
col="time", data=tips, kind="swarm")
<seaborn.axisgrid.FacetGrid at 0xe253160>
参数size、aspect,参见第4章中sns.lmplot()部分
sns.factorplot(x="time", y="total_bill", hue="smoker",
col="day", data=tips, kind="box", size=4, aspect=.5) # size、aspect设置的是每个分面的大小
<seaborn.axisgrid.FacetGrid at 0xe398320>
注意区别于FacetGrid对象,Facet对象用于条件于其他变量的情况,PairGrid对象用于研究多个变量两两关系的情况
参见第4章“多个变量相对于一个变量的回归线”部分,第4章用于连续型变量,这里用于类别型变量,本质相同
g = sns.PairGrid(tips,
x_vars=["smoker", "time", "sex"],
y_vars=["total_bill", "tip"],
aspect=.75, size=3.5)
g.map(sns.violinplot, palette="pastel") # pairplot中kind参数只能设置‘scatter’和‘reg’,所以绘制小提琴图只能用PairGrid对象的map方法
<seaborn.axisgrid.PairGrid at 0xf3690b8>
Subplot grid for plotting conditional relationships.
Subplot grid for plotting pairwise relationships in a dataset.
Grid for drawing a bivariate plot with marginal univariate plots.
第5章“绘制分面类别图”部分
第4章“多个变量相对于一个变量的回归线”部分
第3章“JointGrid对象”部分