How to remove axes spine from the plot in seaborn?


The axes spines, also known as the axis lines, which are the lines that define the borders or boundaries of a plot's coordinate system. In a two-dimensional plot, there are typically four axes spines such as top, bottom, left, and right. These spines create the framework for the plot and serve as reference lines for the data points.

Each spine represents one of the four sides of the plot. The top spine runs horizontally across the top, the bottom spine runs horizontally across the bottom, the left spine runs vertically along the left side, and the right spine runs vertically along the right side.

The axes spines provide a visual reference for the data points and assist in interpreting the plot. They can also be customized in terms of visibility, line style, line width, and color to enhance the overall appearance and emphasize specific aspects of the plot.

Removing axes spine from the plot

Removing or modifying the axes spines can be useful for aesthetic purposes or when we want to emphasize certain features of the plot, such as removing unnecessary clutter or focusing attention on the data itself.

To remove the axes spines from a plot created with Seaborn, we can utilize the matplotlib library, which is the base for the Seaborn library. Matplotlib provides various customization options, including the ability to modify the appearance of the axes spines.

The below are step-by-step guide on how to remove the axes spines from a Seaborn plot.

Import the required libraries

First we have to import all the necessary librares to plot the data.

import seaborn as sns
import matplotlib.pyplot as plt

Create a Seaborn plot

Now we have to create a plot with axes spines for the sample data using the seaborn library along with matplotlib.

Here we are using the Iris dataset available in seaborn library and plotting the scatterplot for the columns sepal_length and sepal_width using the scatterplot() function.

Example

import seaborn as sns
import matplotlib.pyplot as plt

# Generate some example data
data = sns.load_dataset("iris")
# Create a Seaborn plot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)
plt.show()

Output

Access the axes object

For getting the current axes object we will use the function gca() of the matplotlib library.

# Get the current axes object
ax = plt.gca()

Remove the desired spines

Now after getting the axes spines, now we are setting the spines visibility depending upon our requirement and usage with the help of True and False. If we define True, then the axes spine will be visible otherwise it will be removed.

Now we are removing the axes spines of top and right by setting them to False.

Example

import seaborn as sns
import matplotlib.pyplot as plt

# Generate some example data
data = sns.load_dataset("iris")
# Create a Seaborn plot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)

# Get the current axes object
ax = plt.gca()

# Remove the top and right spines
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)

plt.show()

Output

Customize the remaining spines

In this step we are customizing the remaining axes spines bottom and left by setting the line width and line style.

Example

import seaborn as sns
import matplotlib.pyplot as plt

# Generate some example data
data = sns.load_dataset("iris")
# Create a Seaborn plot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)

# Get the current axes object
ax = plt.gca()

# Customize the appearance of the remaining spines
ax.spines["bottom"].set_linewidth(0.5)  # Set the linewidth of the bottom spine
ax.spines["left"].set_linestyle("--")  # Set the linestyle of the left spine

plt.show()

Output

Note −

The specific customization options for the spines, such as linewidth, linestyle, color, etc., can be adjusted to meet our requirements.

Updated on: 02-Aug-2023

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements