# seaborn.swarmplot
> 譯者:[LIJIANcoder97](https://github.com/LIJIANcoder97)
```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)
```
繪制具有非重疊點的分類散點圖。
此功能類似于 [`stripplot()`](seaborn.stripplot.html#seaborn.stripplot "seaborn.stripplot"),,但調整點(僅沿分類軸),以便它們不重疊。 這樣可以更好地表示值的分布,但不能很好地擴展到大量觀察值。這種情節有時被稱為“詛咒”
一個群體圖可以單獨繪制,但如果你想要顯示所有觀察結果以及底層分布的一些表示,它也是一個盒子或小提琴圖的良好補充。
正確排列點需要在數據和點坐標之間進行精確轉換。這意味著必須在繪制繪圖之前設置非默認軸限制。
輸入數據可以以多種格式傳遞,包括:
* 表示為列表,numpy arrays 或 pandas Series objects 直接傳遞給`x`,`y`和/或`hue`參數。
* “長格式” DataFrame, `x`,`y`和`hue`變量將決定數據的繪制方式
* “寬格式”DataFrame,用于繪制每個數字列。
* 一個數組或向量列表。
在大多數情況下,可以使用 numpy 或 Python 對象,但最好使用 pandas 對象,因為關聯的名稱將用于注釋軸。此外,您可以使用分類類型來分組變量來控制繪圖元素的順序。
此函數始終將其中一個變量視為分類,并在相關軸上的序數位置(0,1,... n)處繪制數據,即使數據具有數字或日期類型也是如此
有關更多信息,請參閱[教程](http://seaborn.pydata.org/tutorial/categorical.html#categorical-tutorial)。
參數:`x, y, hue`:`數據`或矢量數據中的變量名稱,可選
> 用于繪制長格式數據的輸入。查看解釋示例。
`data`:DataFrame, array, or 或數組列表, 可選
> 用于繪圖的數據集。 如果 `x` 和 `y` 是不存在的, 會被解釋成 wide-form. 否則會被解釋成 long-form.
`order, hue_order`:字符串列表,可選
> 命令繪制分類級別,否則從數據對象推斷級別。
`dodge`:布爾,可選
> 使用`hue`嵌套時,將其設置為`True`將沿著分類軸分離不同色調級別的條帶。 否則,每個級別的點將繪制在一個群中。
`orient`:“v” | “h”, 可選
> 圖的方向(垂直或水平)。這通常是從輸入變量的 dtype 推斷出來的,但可用于指定“分類”變量何時是數字或何時繪制寬格式數據。
`color`:matplotlib color, 可選
> 所有元素的顏色,或漸變調色板的種子。
`palette`:調色板名稱, list, or dict, 可選
> 用于`hue`變量的不同級別的顏色。應該是[`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"),可以解釋的東西,或者是將色調級別映射到 matplotlib 顏色的字典。
`size`:float, 可選
> 標記的直徑,以點為單位。 (盡管`plt.scatter`用于繪制點,但此處的`size`參數采用“普通”標記大小而不是大小^ 2,如`plt.scatter`。
`edgecolor`:matplotlib color, “灰色”是特殊的,可選
> 每個點周圍線條的顏色。如果傳遞`"gray"`,則亮度由用于點體的調色板決定。
`linewidth`:float, 可選
> 構圖元素的灰線寬度。
`ax`:matplotlib Axes, 可選
> Axes 對象將繪圖繪制到,否則使用當前軸。
返回值:`ax`:matplotlib Axes
> 返回 Axes 對象,并在其上繪制繪圖。
參看
帶有類似 API 的傳統盒須圖。框圖和核密度估計的組合。散點圖,其中一個變量是分類的。可以與其他圖一起使用以顯示每個觀察結果。使用類組合分類圖:<cite>FacetGrid</cite>。
例
繪制單個水平群圖:
```py
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.swarmplot(x=tips["total_bill"])
```

通過分類變量對群組進行分組:
```py
>>> ax = sns.swarmplot(x="day", y="total_bill", data=tips)
```

繪制水平群:
```py
>>> ax = sns.swarmplot(x="total_bill", y="day", data=tips)
```

使用第二個分類變量為點著色:
```py
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips)
```

沿著分類軸拆分 `hue` 變量的每個級別:
```py
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="smoker",
... data=tips, palette="Set2", dodge=True)
```

通過傳遞顯式順序來控制 swarm 順序:
```py
>>> ax = sns.swarmplot(x="time", y="tip", data=tips,
... order=["Dinner", "Lunch"])
```

繪制使用更大的點
```py
>>> ax = sns.swarmplot(x="time", y="tip", data=tips, size=6)
```

在箱形圖上繪制大量觀察結果:
```py
>>> ax = sns.boxplot(x="tip", y="day", data=tips, whis=np.inf)
>>> ax = sns.swarmplot(x="tip", y="day", data=tips, color=".2")
```

在小提琴圖的頂部畫出大量的觀察結果:
```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")
```

使用[`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 的變量順序的同步
```py
>>> g = sns.catplot(x="sex", y="total_bill",
... hue="smoker", col="time",
... data=tips, kind="swarm",
... height=4, aspect=.7);
```

- seaborn 0.9 中文文檔
- Seaborn 簡介
- 安裝和入門
- 可視化統計關系
- 可視化分類數據
- 可視化數據集的分布
- 線性關系可視化
- 構建結構化多圖網格
- 控制圖像的美學樣式
- 選擇調色板
- seaborn.relplot
- seaborn.scatterplot
- seaborn.lineplot
- seaborn.catplot
- seaborn.stripplot
- seaborn.swarmplot
- seaborn.boxplot
- seaborn.violinplot
- seaborn.boxenplot
- seaborn.pointplot
- seaborn.barplot
- seaborn.countplot
- seaborn.jointplot
- seaborn.pairplot
- seaborn.distplot
- seaborn.kdeplot
- seaborn.rugplot
- seaborn.lmplot
- seaborn.regplot
- seaborn.residplot
- seaborn.heatmap
- seaborn.clustermap
- seaborn.FacetGrid
- seaborn.FacetGrid.map
- seaborn.FacetGrid.map_dataframe
- seaborn.PairGrid
- seaborn.PairGrid.map
- seaborn.PairGrid.map_diag
- seaborn.PairGrid.map_offdiag
- seaborn.PairGrid.map_lower
- seaborn.PairGrid.map_upper
- seaborn.JointGrid
- seaborn.JointGrid.plot
- seaborn.JointGrid.plot_joint
- seaborn.JointGrid.plot_marginals
- seaborn.set
- seaborn.axes_style
- seaborn.set_style
- seaborn.plotting_context
- seaborn.set_context
- seaborn.set_color_codes
- seaborn.reset_defaults
- seaborn.reset_orig
- seaborn.set_palette
- seaborn.color_palette
- seaborn.husl_palette
- seaborn.hls_palette
- seaborn.cubehelix_palette
- seaborn.dark_palette
- seaborn.light_palette
- seaborn.diverging_palette
- seaborn.blend_palette
- seaborn.xkcd_palette
- seaborn.crayon_palette
- seaborn.mpl_palette
- seaborn.choose_colorbrewer_palette
- seaborn.choose_cubehelix_palette
- seaborn.choose_light_palette
- seaborn.choose_dark_palette
- seaborn.choose_diverging_palette
- seaborn.load_dataset
- seaborn.despine
- seaborn.desaturate
- seaborn.saturate
- seaborn.set_hls_values