swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, split=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(), 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, although it does not
scale as well to large numbers of observations (both in terms of the
ability to show all the points and in terms of the computation needed
to arrange them).
This style of plot is often 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.
Note that arranging the points properly requires an accurate transformation between data and point coordinates. This means that non-default axis limits should be set before drawing the swarm plot.
Input data can be passed in a variety of formats, including:
huevariables will determine how the data are plotted.
plt.boxplot(e.g. a 2d 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.
x, y, hue : names of variables in
data : DataFrame, array, or list of arrays, optional
order, hue_order : lists of strings, optional
split : bool, optional
orient : “v” | “h”, optional
color : matplotlib color, optional
palette : seaborn color palette or dict, optional
size : float, optional
edgecolor : matplotlib color, “gray” is special-cased, optional
linewidth : float, optional
ax : matplotlib Axes, optional
ax : matplotlib Axes
Draw a single horizontal swarm plot:
>>> import seaborn as sns >>> sns.set_style("whitegrid") >>> tips = sns.load_dataset("tips") >>> ax = sns.swarmplot(x=tips["total_bill"])
Group the swarms by a categorical variable:
>>> ax = sns.swarmplot(x="day", y="total_bill", data=tips)
Draw horizontal swarms:
>>> ax = sns.swarmplot(x="total_bill", y="day", data=tips)
Color the points using a second categorical variable:
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips)
Split each level of the
hue variable along the categorical axis:
>>> ax = sns.swarmplot(x="day", y="total_bill", hue="smoker", ... data=tips, palette="Set2", split=True)
Control swarm order by passing an explicit order:
>>> ax = sns.swarmplot(x="time", y="tip", data=tips, ... order=["Dinner", "Lunch"])
Plot using larger points:
>>> ax = sns.swarmplot(x="time", y="tip", data=tips, size=6)
Draw swarms of observations on top of a box plot:
>>> ax = sns.boxplot(x="tip", y="day", data=tips, whis=np.inf) >>> ax = sns.swarmplot(x="tip", y="day", data=tips)
Draw swarms of observations on top of a violin plot:
>>> 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")