15.md 6.7 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# seaborn.swarmplot

```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)
```

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

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

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

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

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

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

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

李健 已提交
25
有关更多信息,请参阅[教程](../tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
26

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

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

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

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

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

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

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

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

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

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

李健 已提交
47
`color`:matplotlib color, 可选
W
init  
wizardforcel 已提交
48

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

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

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

李健 已提交
55
`size`:float, 可选
W
init  
wizardforcel 已提交
56

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

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

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

李健 已提交
63
`linewidth`:float, 可选
W
init  
wizardforcel 已提交
64

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

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

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

W
wizardforcel 已提交
71 72

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

李健 已提交
74
> 返回Axes对象,并在其上绘制绘图。
W
init  
wizardforcel 已提交
75

W
wizardforcel 已提交
76

W
init  
wizardforcel 已提交
77

李健 已提交
78
参看
W
init  
wizardforcel 已提交
79

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

李健 已提交
82

W
init  
wizardforcel 已提交
83

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

```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)

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

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

```

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

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

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

```

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

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

```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)

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

```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)

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

```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)

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

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

```

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

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

```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)

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

```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)

李健 已提交
173
使用[`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 已提交
174 175 176 177 178 179 180 181 182

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

```

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