Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove axes spine from the plot in seaborn?
The axes spines, also known as the axis lines, 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: 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.
Why Remove Axes Spines?
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. This creates a cleaner, more modern look often preferred in data visualization.
Basic Plot with Default Spines
Let's start by creating a basic Seaborn plot using the Iris dataset to see the default spines ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample data
data = sns.load_dataset("iris")
# Create a basic scatterplot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)
plt.title("Scatterplot with Default Spines")
plt.show()
This plot shows all four spines (top, bottom, left, right) around the plot area.
Method 1: Removing Specific Spines
We can remove individual spines by accessing the current axes object and setting specific spines to invisible ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample data
data = sns.load_dataset("iris")
# Create a scatterplot
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.title("Plot with Top and Right Spines Removed")
plt.show()
Method 2: Using Seaborn's despine() Function
Seaborn provides a convenient despine() function to remove spines more easily ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample data
data = sns.load_dataset("iris")
# Create a scatterplot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)
# Remove top and right spines using despine()
sns.despine()
plt.title("Plot Using sns.despine()")
plt.show()
Method 3: Removing All Spines
To create a completely borderless plot, we can remove all four spines ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample data
data = sns.load_dataset("iris")
# Create a scatterplot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)
# Remove all spines
sns.despine(top=True, right=True, left=True, bottom=True)
plt.title("Plot with All Spines Removed")
plt.show()
Customizing Remaining Spines
After removing unwanted spines, we can customize the appearance of the remaining ones ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample data
data = sns.load_dataset("iris")
# Create a scatterplot
sns.scatterplot(x="sepal_length", y="sepal_width", data=data)
# Get the current axes object
ax = plt.gca()
# Remove top and right spines
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# Customize remaining spines
ax.spines["bottom"].set_linewidth(2)
ax.spines["left"].set_linestyle("--")
ax.spines["left"].set_color("gray")
plt.title("Plot with Customized Remaining Spines")
plt.show()
Common Spine Customization Options
| Method | Description | Example |
|---|---|---|
set_visible(False) |
Removes the spine completely | ax.spines["top"].set_visible(False) |
sns.despine() |
Quick removal of top/right spines | sns.despine(left=True, bottom=True) |
set_linewidth() |
Changes spine thickness | ax.spines["bottom"].set_linewidth(2) |
set_linestyle() |
Changes spine style | ax.spines["left"].set_linestyle("--") |
Conclusion
Removing axes spines in Seaborn can significantly improve plot aesthetics. Use sns.despine() for quick spine removal or ax.spines[].set_visible(False) for more precise control. Customize remaining spines with linewidth, color, and style properties to create professional-looking visualizations.
