Seaborn.despine() method



The spine plots are the plots which are plotted using the tick marks or dots. These plots don’t have any boundaries. The Seborn.despine() method helps to remove the top and right spines in a plot.

Syntax

Following is a syntax of the seaborn.despine() method −

seaborn.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)

Parameters

The parameters of the seaborn.despine() method are described below.

S.No Name and Description
1 Fig

This is a matplotlib figure and it is an optional parameter that despines the axes of the figure passed. If none it uses the current figure.

2 Ax

This is another optional parameter that takes matplotlib axes as inputs and the axes passed are despined/removed.

3 Top,right,left,bottom

This is an optional parameter that takes a Boolean value and remove that particular spine that has a true value.

4 Offset

This is another optional parameter that takes integer or a dictionary as input and it is the value with which the spines should be moved away from the axes. A dictionary can be used to specify the offset values for all the sides.

5 Trim

This is an optional parameter that takes Boolean values and if it is true it limits spines to smallest and largest major tick one ach non-despined axis.

Return Value

This method returns a none object. Let us load the seaborn library and the dataset before moving on to developing the plots.

Loading the seaborn library

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 as few examples.

Example 1

In this example, we will see how the despine method works. We will use the titanic dataset in this article and draw a stripplot of the data from the titanic dataset and then we will apply the despine() function on top of the function. 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.stripplot(x="age", y="who", hue="alive",data=titanic)
sns.despine()
plt.show()

Output

The output produced is a plot with no top and right spines.

seaborn_despine_method

Example 2

In this example, we will pass a few optional parameter and notice the change in the graph. We shall use the top, right, bottom and left parameters and pass different boolena values to them and notice the change in the graph.

In the below line of code we will pass true to top and false tot hr right parameter.

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.stripplot(x="age", y="who", hue="alive",data=titanic)
sns.despine(top=True,right=False)
plt.show()

Output

The output is shown below −

seaborn_despine

Example 3

In this example, we will pass left as False and bottom, right and top as true and notice the change in the plot. 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.stripplot(x="age", y="who", hue="alive",data=titanic)
sns.despine(top=True,right=True, bottom=True, left=False)
plt.show()

Output

The output is as shown below −

despine

Example 4

Now, we will pass the offset values and these values will move the spines away from the plot. We will pass a dictionary with key value pairs as top and thre vaue to move the spine away by for all the 4 sides of the plot.

Since we are passing a dictionary we will need the top and right spines. By default, despine method eliminates these two so, we will ensure that it does not do that by passing false to top and right parameters.

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.stripplot(x="age", y="who", hue="alive",data=titanic)
sns.despine(offset={"top":2,"bottom":5,"right":6,"left":4},top=False,right=False)
plt.show()

Output

The output is as follows −

despine_method
seaborn_utility_functions_introduction.htm
Advertisements