15.md 7.5 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 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)
```

Draw a categorical scatterplot with non-overlapping points.

This function is similar to [`stripplot()`](seaborn.stripplot.html#seaborn.stripplot "seaborn.stripplot"), but the points are adjusted (only along the categorical axis) so that they don’t overlap. This gives a better representation of the distribution of values, but it does not scale well to large numbers of observations. This style of plot is sometimes called a “beeswarm”.

A swarm plot can be drawn on its own, but it is also a good complement to a box or violin plot in cases where you want to show all observations along with some representation of the underlying distribution.

Arranging the points properly requires an accurate transformation between data and point coordinates. This means that non-default axis limits must be set _before_ drawing the plot.

Input data can be passed in a variety of formats, including:

*   Vectors of data represented as lists, numpy arrays, or pandas Series objects passed directly to the `x`, `y`, and/or `hue` parameters.
*   A “long-form” DataFrame, in which case the `x`, `y`, and `hue` variables will determine how the data are plotted.
*   A “wide-form” DataFrame, such that each numeric column will be plotted.
*   An array or list of vectors.

In most cases, it is possible to use numpy or Python objects, but pandas objects are preferable because the associated names will be used to annotate the axes. Additionally, you can use Categorical types for the grouping variables to control the order of plot elements.

This function always treats one of the variables as categorical and draws data at ordinal positions (0, 1, … n) on the relevant axis, even when the data has a numeric or date type.

See the [tutorial](../tutorial/categorical.html#categorical-tutorial) for more information.

W
wizardforcel 已提交
28
参数:`x, y, hue`:names of variables in `data` or vector data, optional
W
init  
wizardforcel 已提交
29 30 31

> Inputs for plotting long-form data. See examples for interpretation.

W
wizardforcel 已提交
32
`data`:DataFrame, array, or list of arrays, optional
W
init  
wizardforcel 已提交
33 34 35

> Dataset for plotting. If `x` and `y` are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

W
wizardforcel 已提交
36
`order, hue_order`:lists of strings, optional
W
init  
wizardforcel 已提交
37 38 39

> Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

W
wizardforcel 已提交
40
`dodge`:bool, optional
W
init  
wizardforcel 已提交
41 42 43

> When using `hue` nesting, setting this to `True` will separate the strips for different hue levels along the categorical axis. Otherwise, the points for each level will be plotted in one swarm.

W
wizardforcel 已提交
44
`orient`:“v” | “h”, optional
W
init  
wizardforcel 已提交
45 46 47

> Orientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables, but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.

W
wizardforcel 已提交
48
`color`:matplotlib color, optional
W
init  
wizardforcel 已提交
49 50 51

> Color for all of the elements, or seed for a gradient palette.

W
wizardforcel 已提交
52
`palette`:palette name, list, or dict, optional
W
init  
wizardforcel 已提交
53 54 55

> Colors to use for the different levels of the `hue` variable. Should be something that can be interpreted by [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), or a dictionary mapping hue levels to matplotlib colors.

W
wizardforcel 已提交
56
`size`:float, optional
W
init  
wizardforcel 已提交
57 58 59

> Diameter of the markers, in points. (Although `plt.scatter` is used to draw the points, the `size` argument here takes a “normal” markersize and not size^2 like `plt.scatter`.

W
wizardforcel 已提交
60
`edgecolor`:matplotlib color, “gray” is special-cased, optional
W
init  
wizardforcel 已提交
61 62 63

> Color of the lines around each point. If you pass `"gray"`, the brightness is determined by the color palette used for the body of the points.

W
wizardforcel 已提交
64
`linewidth`:float, optional
W
init  
wizardforcel 已提交
65 66 67

> Width of the gray lines that frame the plot elements.

W
wizardforcel 已提交
68
`ax`:matplotlib Axes, optional
W
init  
wizardforcel 已提交
69 70 71

> Axes object to draw the plot onto, otherwise uses the current Axes.

W
wizardforcel 已提交
72 73

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

> Returns the Axes object with the plot drawn onto it.

W
wizardforcel 已提交
77

W
init  
wizardforcel 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

See also

A traditional box-and-whisker plot with a similar API.A combination of boxplot and kernel density estimation.A scatterplot where one variable is categorical. Can be used in conjunction with other plots to show each observation.Combine a categorical plot with a class:<cite>FacetGrid</cite>.

Examples

Draw a single horizontal swarm plot:

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

Group the swarms by a categorical variable:

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

```

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

Draw horizontal swarms:

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

```

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

Color the points using a second categorical variable:

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

Split each level of the `hue` variable along the categorical axis:

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

Control swarm order by passing an explicit order:

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

Plot using larger points:

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

```

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

Draw swarms of observations on top of a box plot:

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

Draw swarms of observations on top of a violin plot:

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

Use [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") to combine a [`swarmplot()`](#seaborn.swarmplot "seaborn.swarmplot") and a [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"). This allows grouping within additional categorical variables. Using [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") is safer than using [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") directly, as it ensures synchronization of variable order across facets:

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

```

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