13.md 8.5 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.catplot

W
credit  
wizardforcel 已提交
3 4
> 译者:[LIJIANcoder97](https://github.com/LIJIANcoder97)

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.catplot(x=None, y=None, hue=None, data=None, row=None, col=None, col_wrap=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, order=None, hue_order=None, row_order=None, col_order=None, kind='strip', height=5, aspect=1, orient=None, color=None, palette=None, legend=True, legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, **kwargs)
```

W
wizardforcel 已提交
9
seaborn.catplot 是一个将分类图绘制到 FacetGrid 上图级别接口。
W
init  
wizardforcel 已提交
10

李健 已提交
11
这个函数可以访问多个轴级功能,这些轴级功能通过不同的可视化图表展示数字和一个或多个分类变量的关系。`kind` 参数可以选择的轴级基础函数有:
W
init  
wizardforcel 已提交
12

李健 已提交
13
分类散点图:
W
init  
wizardforcel 已提交
14 15 16 17

*   [`stripplot()`](seaborn.stripplot.html#seaborn.stripplot "seaborn.stripplot") (with `kind="strip"`; the default)
*   [`swarmplot()`](seaborn.swarmplot.html#seaborn.swarmplot "seaborn.swarmplot") (with `kind="swarm"`)

李健 已提交
18
分类分布图:
W
init  
wizardforcel 已提交
19 20 21 22 23

*   [`boxplot()`](seaborn.boxplot.html#seaborn.boxplot "seaborn.boxplot") (with `kind="box"`)
*   [`violinplot()`](seaborn.violinplot.html#seaborn.violinplot "seaborn.violinplot") (with `kind="violin"`)
*   [`boxenplot()`](seaborn.boxenplot.html#seaborn.boxenplot "seaborn.boxenplot") (with `kind="boxen"`)

李健 已提交
24
分类估计图:
W
init  
wizardforcel 已提交
25 26 27 28 29

*   [`pointplot()`](seaborn.pointplot.html#seaborn.pointplot "seaborn.pointplot") (with `kind="point"`)
*   [`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot") (with `kind="bar"`)
*   [`countplot()`](seaborn.countplot.html#seaborn.countplot "seaborn.countplot") (with `kind="count"`)

李健 已提交
30
额外的关键字参数将传递给基础函数,因此,您应参阅每个文档,以查看特定类型的选项.
W
init  
wizardforcel 已提交
31

W
wizardforcel 已提交
32
请注意,与直接使用轴级函数不同, 数据必须在长格式 DataFrame 中传递,并通过将字符串传递给 `x`, `y`, `hue`, 等指定的变量.
W
init  
wizardforcel 已提交
33

李健 已提交
34
与基础绘图函数的情况一样, 如果变量有 `categorical` 数据类型, 则将从对象推断出分类变量的级别及其顺序。否则,您可能必须使用更改 dataframe 排序或使用函数参数(`orient`, `order`, `hue_order`, etc.) 来正确设置绘图。
W
init  
wizardforcel 已提交
35

李健 已提交
36
此函数始终将其中一个变量视为分类,并在相关轴上的序数位置(0,1,... n)处绘制数据,即使数据具有数字或日期类型。
W
init  
wizardforcel 已提交
37

沈宇 已提交
38
有关更多信息,请参考 [tutorial](http://seaborn.pydata.org/tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
39

李健 已提交
40
绘图后,返回带有绘图的 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"),可以直接用于调整绘图细节或添加其他图层。
W
init  
wizardforcel 已提交
41

李健 已提交
42
参数:`x, y, hue``data` names 中的变量名称
W
init  
wizardforcel 已提交
43

李健 已提交
44
> 用于绘制长格式数据的输入。查看解释示例
W
init  
wizardforcel 已提交
45

W
wizardforcel 已提交
46
`data`:DataFrame
W
init  
wizardforcel 已提交
47

李健 已提交
48
> 用于绘图的长形(整洁)数据集。每列应对应一个变量,每行应对应一个观察。
W
init  
wizardforcel 已提交
49

李健 已提交
50
`row, col``data` 中的变量名称, 可选
W
init  
wizardforcel 已提交
51

李健 已提交
52
> 分类变量将决定网格的分面。
W
init  
wizardforcel 已提交
53

李健 已提交
54
`col_wrap`:int, 可选
W
init  
wizardforcel 已提交
55

李健 已提交
56
> 以此宽度“包裹”列变量,以便列面跨越多行。 与`行`方面不兼容。
W
init  
wizardforcel 已提交
57

李健 已提交
58
`estimator`:可调用的映射向量 -&gt; 标量,可选
W
init  
wizardforcel 已提交
59

李健 已提交
60
> 在每个分类箱内估计的统计函数。
W
init  
wizardforcel 已提交
61

W
wizardforcel 已提交
62
`ci`:float 或“sd”或 None,可选
W
init  
wizardforcel 已提交
63

李健 已提交
64
> 在估计值附近绘制置信区间的大小。如果是“sd”,则跳过自举(bootstrapping)并绘制观察的标准偏差。None,如果为`None`,则不执行自举,并且不会绘制错误条。
W
init  
wizardforcel 已提交
65

李健 已提交
66
`n_boot`:int,可选
W
init  
wizardforcel 已提交
67

李健 已提交
68
> 计算置信区间时使用的引导程序迭代次数。
W
init  
wizardforcel 已提交
69

李健 已提交
70
`units``数据`或矢量数据中变量的名称,可选
W
init  
wizardforcel 已提交
71

李健 已提交
72
>采样单元的标识符,用于执行多级引导程序并考虑重复测量设计。
W
init  
wizardforcel 已提交
73

李健 已提交
74
`order, hue_order`:字符串列表,可选
W
init  
wizardforcel 已提交
75

李健 已提交
76
> 命令绘制分类级别,否则从数据对象推断级别。
W
init  
wizardforcel 已提交
77

李健 已提交
78
`row_order, col_order`:字符串列表,可选
W
init  
wizardforcel 已提交
79

李健 已提交
80
>命令组织网格的行和/或列,否则从数据对象推断命令。
W
init  
wizardforcel 已提交
81

李健 已提交
82
`kind`:字符串,可选
W
init  
wizardforcel 已提交
83

李健 已提交
84
>要绘制的绘图类型(对应于分类绘图功能的名称。选项包括:“点”,“条形”,“条形”,“群”,“框”,“小提琴”或“盒子”。
W
init  
wizardforcel 已提交
85

李健 已提交
86
`height`:标量,可选
W
init  
wizardforcel 已提交
87

李健 已提交
88
> 每个刻面的高度(以英寸为单位)。另见: `aspect`。
W
init  
wizardforcel 已提交
89

李健 已提交
90
`aspect`:标量,可选
W
init  
wizardforcel 已提交
91

李健 已提交
92
> 每个面的纵横比,因此`aspect * height`给出每个面的宽度,单位为英寸。
W
init  
wizardforcel 已提交
93

李健 已提交
94
`orient`:“v” &#124; “h”, 可选
W
init  
wizardforcel 已提交
95

W
wizardforcel 已提交
96
> 图的方向(垂直或水平)。这通常是从输入变量的 dtype 推断出来的,但可用于指定“分类”变量何时是数字或何时绘制宽格式数据。
W
init  
wizardforcel 已提交
97

W
wizardforcel 已提交
98
`color`:matplotlib 颜色,可选
W
init  
wizardforcel 已提交
99

李健 已提交
100
> 所有元素的颜色,或渐变调色板的种子。
W
init  
wizardforcel 已提交
101

李健 已提交
102
`palette`:调色板名称,列表或字典,可选
W
init  
wizardforcel 已提交
103

W
wizardforcel 已提交
104
> 用于色调变量的不同级别的颜色。应该是 [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), 可以解释的东西,或者是将色调级别映射到 matplotlib 颜色的字典。
W
init  
wizardforcel 已提交
105

李健 已提交
106
`legend`:bool, 可选
W
init  
wizardforcel 已提交
107

李健 已提交
108
>  如果为 `True` 并且存在`hue`变量,则在图上绘制图例。t.
W
init  
wizardforcel 已提交
109

李健 已提交
110
`legend_out`:bool, 可选
W
init  
wizardforcel 已提交
111

李健 已提交
112
>  如果为`True`,则图形尺寸将被扩展,图例将绘制在中间右侧的图形之外。
W
init  
wizardforcel 已提交
113 114


李健 已提交
115
`share{x,y}`:bool, ‘col’, 或 ‘row’ 可选
W
init  
wizardforcel 已提交
116

W
wizardforcel 已提交
117
> 如果为 true,则 facet 将跨行跨越列和/或 x 轴共享 y 轴。
W
init  
wizardforcel 已提交
118

李健 已提交
119
`margin_titles`:bool, 可选
W
init  
wizardforcel 已提交
120

李健 已提交
121
> 如果为`True`,则行变量的标题将绘制在最后一列的右侧。此选项是实验性的,可能无法在所有情况下使用。
W
init  
wizardforcel 已提交
122

李健 已提交
123
`facet_kws`:dict, 可选
W
init  
wizardforcel 已提交
124

李健 已提交
125
> 传递给[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid")的其他关键字参数的字典。
W
init  
wizardforcel 已提交
126

李健 已提交
127 128 129
`kwargs`:key, value 配对

> 其他关键字参数将传递给基础绘图函数。
W
init  
wizardforcel 已提交
130

W
wizardforcel 已提交
131 132

返回值:`g`[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid")
W
init  
wizardforcel 已提交
133

李健 已提交
134
> 返回[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid")对象及其上的绘图以进一步调整。
W
init  
wizardforcel 已提交
135

W
wizardforcel 已提交
136

W
init  
wizardforcel 已提交
137

李健 已提交
138
例子
W
init  
wizardforcel 已提交
139

李健 已提交
140
绘制单个构面以使用[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid")图例放置:
W
init  
wizardforcel 已提交
141 142 143 144 145 146 147 148 149 150 151

```py
>>> import seaborn as sns
>>> sns.set(style="ticks")
>>> exercise = sns.load_dataset("exercise")
>>> g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)

```

![http://seaborn.pydata.org/_images/seaborn-catplot-1.png](img/0b12363978e10369fc98bd33cb536d85.jpg)

李健 已提交
152
使用不同的绘图类型可视化相同的数据:
W
init  
wizardforcel 已提交
153 154 155 156 157 158 159 160 161

```py
>>> g = sns.catplot(x="time", y="pulse", hue="kind",
...                data=exercise, kind="violin")

```

![http://seaborn.pydata.org/_images/seaborn-catplot-2.png](img/edd1fe3b83d3ec7ff2a1310bc2b87b8c.jpg)

李健 已提交
162
沿列的方面显示第三个分类变量:
W
init  
wizardforcel 已提交
163 164 165 166 167 168 169 170 171

```py
>>> g = sns.catplot(x="time", y="pulse", hue="kind",
...                 col="diet", data=exercise)

```

![http://seaborn.pydata.org/_images/seaborn-catplot-3.png](img/f54e97484ce6dfcae6e1ceafe18503af.jpg)

李健 已提交
172
为构面使用不同的高度和宽高比:
W
init  
wizardforcel 已提交
173 174 175 176 177 178 179 180 181 182

```py
>>> g = sns.catplot(x="time", y="pulse", hue="kind",
...                 col="diet", data=exercise,
...                 height=5, aspect=.8)

```

![http://seaborn.pydata.org/_images/seaborn-catplot-4.png](img/ff09acb1a4ecf2c617aa1323f8894fe5.jpg)

李健 已提交
183
创建许多列构面并将它们包装到网格的行中:
W
init  
wizardforcel 已提交
184 185 186 187 188 189 190 191 192 193 194

```py
>>> titanic = sns.load_dataset("titanic")
>>> g = sns.catplot("alive", col="deck", col_wrap=4,
...                 data=titanic[titanic.deck.notnull()],
...                 kind="count", height=2.5, aspect=.8)

```

![http://seaborn.pydata.org/_images/seaborn-catplot-5.png](img/94e545c07037b66573b94c5d43e41899.jpg)

李健 已提交
195
水平绘图并将其他关键字参数传递给绘图函数:
W
init  
wizardforcel 已提交
196 197 198 199 200 201 202 203 204 205 206 207

```py
>>> g = sns.catplot(x="age", y="embark_town",
...                 hue="sex", row="class",
...                 data=titanic[titanic.embark_town.notnull()],
...                 orient="h", height=2, aspect=3, palette="Set3",
...                 kind="violin", dodge=True, cut=0, bw=.2)

```

![http://seaborn.pydata.org/_images/seaborn-catplot-6.png](img/141f4c98341da1bc3beb4dc78e30df08.jpg)

李健 已提交
208
 使用返回的[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 上的方法来调整演示文稿:
W
init  
wizardforcel 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222

```py
>>> g = sns.catplot(x="who", y="survived", col="class",
...                 data=titanic, saturation=.5,
...                 kind="bar", ci=None, aspect=.6)
>>> (g.set_axis_labels("", "Survival Rate")
...   .set_xticklabels(["Men", "Women", "Children"])
...   .set_titles("{col_name}  {col_var}")
...   .set(ylim=(0, 1))
...   .despine(left=True))  
<seaborn.axisgrid.FacetGrid object at 0x...>

```

李健 已提交
223
![http://seaborn.pydata.org/_images/seaborn-catplot-7.png](img/407bdac663f0df796492a65e635fca81.jpg)