Seaborn.swarmplot() method



Seaborn.swarmplot() method is used to draw a non-overlapping scatter plot where one of the variables is a categorical variable. this function is similar to the seaborn.stripplot() method but the points are adjusted so as to not overlap only on the categorical axis.

This provides a better representation and distribution of values but it is not ideally used for large numbers of observations. The nature of the plot looks like a bee swarm and it is thus called a swarmplot(). Similar to strip plot, seaborn.swarmplot() method can be used to complement other plots such as box or violin plots or on its own. It takes input in the form of wide-form data, long-from datal, arrays or a list of vectors.

Syntax

Following is the syntax of the seaborn.swarmplot() method −

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

Some of the parameters of the method are explained below.

S.No Parameter and Description
1 x,y

These parameters take names of variables as input that plot the long form data.

2 data

This is the data frame that is used to plot graphs.

3 hue

Names of variables in the dataframe that are needed for plotting the graph.

4 linewidth

This parameter takes floating values and determines the width of the gray lines that frame the elements in the plot.

5 dodge

This parameter takes a Boolean value. if we use hue nesting, passing true to this parameter will separate the strips for different hue levels. If False is passed, the points for each level will be plotted on top of each other.

6 orient

It takes values “h” or “v” and the orientation of the graph is determined based on this.

7 color

matplotlib color is taken as input and this determines the color of all the elements.

8 size

this parameter determines the radius of the markers in the plot drawn.

Loading the seaborn library

Let us load the seaborn library and the dataset before moving on to the developing the plots. To load or import the seaborn library the following line of code can be used.

Import seaborn as sns

Loading the dataset

in this article, we will make use of the Titanic dataset inbuilt in the seaborn library. the following command is used to load the dataset.

titanic=sns.load_dataset("titanic")

The below mentioned command is used to view the first 5 rows in the dataset. This enables us to understand what variables can be used to plot a graph.

titanic.head()

the below is the output for the above piece of code.

index,survived,pclass,sex,age,sibsp,parch,fare,embarked,class,who,adult_male,deck,embark_town,alive,alone
0,0,3,male,22.0,1,0,7.25,S,Third,man,true,NaN,Southampton,no,false
1,1,1,female,38.0,1,0,71.2833,C,First,woman,false,C,Cherbourg,yes,false
2,1,3,female,26.0,0,0,7.925,S,Third,woman,false,NaN,Southampton,yes,true

Now that we have loaded the dataset, we will explore a few examples.

Example 1

In this example, we will understand how to plot a simple swarm plot by passing the x,y and data parameters to the seaborn.swarmplot() method. we are using the titanic dataset to understand the working of this method, so the columns who and age from the titanic dataset are passed to the x,y parameters respectively. The below line of code can be used to do so.

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.swarmplot(x="who", y="age", data=titanic)
plt.show()

Output

The plot obtained is attached below.

seaborn swarmplot

Example 2

We will understand the usage of the size parameter of the seaborn.swarmplot() method. This parameter takes floating point value as input and it is an optional parameter. The radius of the markers is effected by the value passed to this parameter.

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.swarmplot(x="age",data=titanic,y="who",size=10)
plt.show()

Output

the output plot produced is as follows,

seaborn swarmplot method

As we can see in the plot, the size of the markers have increased considerably in size and this is because the size parameter was passed the value 10, so the markers have a radius of 10.

Example 3

Now, we will understand how the hue mapping effects the graph/output produced. since we are using the titanic dataset and it has 4 categorical variables, we are passing one of them to either x or y and another to the hue parameter. In this example, we are passing the alive variable to the hue parameter.

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.swarmplot(x="who", y="age",hue="alive", data=titanic)
plt.show()

Output

The code above will obtain the output shown below.

graph output produced

Example 4

In this example we will understand how to use the dodge parameter along with the parameters used above. This parameter takes a boolean value. if we use hue nesting, passing true to this parameter will separate the strips for different hue levels. If False is passed, the points for each level will be plotted on top of each other. The below line of code can be used to plot a graph using the dodge parameter.

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.swarmplot(x="who", y="age", hue="alive",dodge=True,data=titanic)
plt.show()

Output

The output of the above line of code is as follows −

dodge parameter
seaborn_categorial_plots_Introduction.htm
Advertisements