Python Seaborn Categorical scatterplots: Swarm Plot

Swarmplot is similar to a strip plot and a type of scatter plot. It is used for a better representation of categorical values.  It is also used to avoid the overlapping of points. We use seaborn.swarmplot() to create such graphs.

Swarmplot is basically a categorical scatterplot with non-overlapping points.

Swarmplot can be drawn on its own but it is also a good complement to the box and violin plot when we want to show all the observations along with some representation of the underlying distribution.

Syntax

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)

Parameters:

  • x,y, hue: It is input use for plotting long-from of data.
  • data: It is the datasets or dataframe used for plotting the graph.
  • order, hue_order: Order  which is followed to plot the categorical levels.
  • dodge: It is a bool. It is used in nesting hue, If it is set to True will separate the strips for different hue levels along the categorical axis. 

Examples

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


data = sns.load_dataset("exercise")
#simple swarmplot
sns.swarmplot(y='pulse', x='diet', data=data)

Output:

swarm

#using hue
sns.swarmplot(y='pulse', x='diet', hue='kind', data=data)

Output:

hue

#orientation to h
sns.swarmplot(y = "diet", x = "pulse", hue = "time", orient = "h", data = data)

Output:

orient

#dodge is set to True
sns.swarmplot(y='pulse', x='diet', hue='kind', data=data,dodge=True)

Output:

dodge

#swarmplot with boxplot
plt.figure(figsize=(10,5))
sns.boxplot(y='pulse', x='diet', hue='kind', data=data)
sns.swarmplot(y='pulse', x='diet', hue='kind', data=data,color='black',alpha=0.5,linewidth=1)

Output:

box

#swarmplot with violinplot
plt.figure(figsize=(10,8))
sns.violinplot(y='pulse', x='diet', hue='kind', data=data)
sns.swarmplot(y='pulse', x='diet', hue='kind', data=data,color='black',alpha=0.5,linewidth=1)

Output:

violin

Wed, 03/03/2021 - 02:27

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.