15.md 6.8 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.swarmplot

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

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
```

李健 已提交
9
绘制具有非重叠点的分类散点图。
W
init  
wizardforcel 已提交
10

李健 已提交
11
此功能类似于 [`stripplot()`](seaborn.stripplot.html#seaborn.stripplot "seaborn.stripplot"),,但调整点(仅沿分类轴),以便它们不重叠。 这样可以更好地表示值的分布,但不能很好地扩展到大量观察值。这种情节有时被称为“诅咒”
W
init  
wizardforcel 已提交
12

李健 已提交
13
一个群体图可以单独绘制,但如果你想要显示所有观察结果以及底层分布的一些表示,它也是一个盒子或小提琴图的良好补充。
W
init  
wizardforcel 已提交
14

李健 已提交
15
正确排列点需要在数据和点坐标之间进行精确转换。这意味着必须在绘制绘图之前设置非默认轴限制。
W
init  
wizardforcel 已提交
16

李健 已提交
17
输入数据可以以多种格式传递,包括:
W
wizardforcel 已提交
18
*   表示为列表,numpy arrays 或 pandas Series objects 直接传递给`x``y`和/或`hue`参数。
李健 已提交
19 20 21
*   “长格式” DataFrame, `x``y``hue`变量将决定数据的绘制方式
*   “宽格式”DataFrame,用于绘制每个数字列。
*   一个数组或向量列表。
W
init  
wizardforcel 已提交
22

W
wizardforcel 已提交
23
在大多数情况下,可以使用 numpy 或 Python 对象,但最好使用 pandas 对象,因为关联的名称将用于注释轴。此外,您可以使用分类类型来分组变量来控制绘图元素的顺序。
W
init  
wizardforcel 已提交
24

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

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

李健 已提交
29
参数:`x, y, hue``数据`或矢量数据中的变量名称,可选
W
init  
wizardforcel 已提交
30

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

李健 已提交
33
`data`:DataFrame, array, or 或数组列表, 可选
W
init  
wizardforcel 已提交
34

李健 已提交
35
> 用于绘图的数据集。 如果 `x` 和 `y` 是不存在的, 会被解释成 wide-form. 否则会被解释成 long-form.
W
init  
wizardforcel 已提交
36

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

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

李健 已提交
41
`dodge`:布尔,可选
W
init  
wizardforcel 已提交
42

李健 已提交
43
> 使用`hue`嵌套时,将其设置为`True`将沿着分类轴分离不同色调级别的条带。 否则,每个级别的点将绘制在一个群中。
W
init  
wizardforcel 已提交
44

李健 已提交
45
`orient`:“v” | “h”, 可选
W
init  
wizardforcel 已提交
46

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

李健 已提交
49
`color`:matplotlib color, 可选
W
init  
wizardforcel 已提交
50

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

李健 已提交
53
`palette`:调色板名称, list, or dict, 可选
W
init  
wizardforcel 已提交
54

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

李健 已提交
57
`size`:float, 可选
W
init  
wizardforcel 已提交
58

李健 已提交
59
> 标记的直径,以点为单位。 (尽管`plt.scatter`用于绘制点,但此处的`size`参数采用“普通”标记大小而不是大小^ 2,如`plt.scatter`。
W
init  
wizardforcel 已提交
60

李健 已提交
61
`edgecolor`:matplotlib color, “灰色”是特殊的,可选
W
init  
wizardforcel 已提交
62

李健 已提交
63
> 每个点周围线条的颜色。如果传递`"gray"`,则亮度由用于点体的调色板决定。
W
init  
wizardforcel 已提交
64

李健 已提交
65
`linewidth`:float, 可选
W
init  
wizardforcel 已提交
66

李健 已提交
67
> 构图元素的灰线宽度。
W
init  
wizardforcel 已提交
68

李健 已提交
69
`ax`:matplotlib Axes, 可选
W
init  
wizardforcel 已提交
70

W
wizardforcel 已提交
71
> Axes 对象将绘图绘制到,否则使用当前轴。
W
init  
wizardforcel 已提交
72

W
wizardforcel 已提交
73 74

返回值:`ax`:matplotlib Axes
W
init  
wizardforcel 已提交
75

W
wizardforcel 已提交
76
> 返回 Axes 对象,并在其上绘制绘图。
W
init  
wizardforcel 已提交
77

W
wizardforcel 已提交
78

W
init  
wizardforcel 已提交
79

李健 已提交
80
参看
W
init  
wizardforcel 已提交
81

W
wizardforcel 已提交
82
带有类似 API 的传统盒须图。框图和核密度估计的组合。散点图,其中一个变量是分类的。可以与其他图一起使用以显示每个观察结果。使用类组合分类图:<cite>FacetGrid</cite>
W
init  
wizardforcel 已提交
83

李健 已提交
84

W
init  
wizardforcel 已提交
85

李健 已提交
86
绘制单个水平群图:
W
init  
wizardforcel 已提交
87 88 89 90 91 92 93 94 95 96 97

```py
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.swarmplot(x=tips["total_bill"])

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-1.png](img/900c725fafc0f5e475a98f52f4ed7d04.jpg)

李健 已提交
98
通过分类变量对群组进行分组:
W
init  
wizardforcel 已提交
99 100 101 102 103 104 105 106

```py
>>> ax = sns.swarmplot(x="day", y="total_bill", data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-2.png](img/414037bdbfc9b79cf5f12a30645f7301.jpg)

李健 已提交
107
绘制水平群:
W
init  
wizardforcel 已提交
108 109 110 111 112 113 114 115

```py
>>> ax = sns.swarmplot(x="total_bill", y="day", data=tips)

```

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

李健 已提交
116
使用第二个分类变量为点着色:
W
init  
wizardforcel 已提交
117 118 119 120 121 122 123 124

```py
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-4.png](img/12d5e5950bf28b7027b28766bc41989f.jpg)

李健 已提交
125
沿着分类轴拆分 `hue` 变量的每个级别:
W
init  
wizardforcel 已提交
126 127 128 129 130 131 132 133 134

```py
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="smoker",
...                    data=tips, palette="Set2", dodge=True)

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-5.png](img/6f9585fcbe42e72521292b80b0fdc97a.jpg)

W
wizardforcel 已提交
135
通过传递显式顺序来控制 swarm 顺序:
W
init  
wizardforcel 已提交
136 137 138 139 140 141 142 143 144

```py
>>> ax = sns.swarmplot(x="time", y="tip", data=tips,
...                    order=["Dinner", "Lunch"])

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-6.png](img/73bbf8c6208a6e1c0dda89091dd509a4.jpg)

李健 已提交
145
绘制使用更大的点
W
init  
wizardforcel 已提交
146 147 148 149 150 151 152 153

```py
>>> ax = sns.swarmplot(x="time", y="tip", data=tips, size=6)

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-7.png](img/34aa97c61dd6f6176fa2256880526439.jpg)

李健 已提交
154
在箱形图上绘制大量观察结果:
W
init  
wizardforcel 已提交
155 156 157 158 159 160 161 162 163

```py
>>> ax = sns.boxplot(x="tip", y="day", data=tips, whis=np.inf)
>>> ax = sns.swarmplot(x="tip", y="day", data=tips, color=".2")

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-8.png](img/d992e6312a3ed98025ad0913dbc46228.jpg)

李健 已提交
164
在小提琴图的顶部画出大量的观察结果:
W
init  
wizardforcel 已提交
165 166 167 168 169 170 171 172 173 174

```py
>>> ax = sns.violinplot(x="day", y="total_bill", data=tips, inner=None)
>>> ax = sns.swarmplot(x="day", y="total_bill", data=tips,
...                    color="white", edgecolor="gray")

```

![http://seaborn.pydata.org/_images/seaborn-swarmplot-9.png](img/735aa7eaadb9afb7a47a2d079b28a10b.jpg)

李健 已提交
175
使用[`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 去组合 [`swarmplot()`](#seaborn.swarmplot "seaborn.swarmplot")[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"). 这允许在其他分类变量中进行分组。 使用 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 比直接使用 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 更安全,因为它确保了跨 facet 的变量顺序的同步
W
init  
wizardforcel 已提交
176 177 178 179 180 181 182 183 184

```py
>>> g = sns.catplot(x="sex", y="total_bill",
...                 hue="smoker", col="time",
...                 data=tips, kind="swarm",
...                 height=4, aspect=.7);

```

李健 已提交
185
![http://seaborn.pydata.org/_images/seaborn-swarmplot-10.png](img/7c1bc4a2871b9e0dbe2c23ed05fcae1b.jpg)