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 can seaborn library be used to display data without the background axis spines in Python?
When creating data visualizations with Seaborn, removing background axis spines can make your plots cleaner and more professional. Seaborn's despine() function provides an easy way to remove these spines for a cleaner appearance.
Data visualization is crucial in machine learning and data analysis as it helps understand patterns without complex calculations. The despine() function removes the top and right axis spines by default, creating a more minimalist look.
Basic Usage of despine()
Here's how to create a plot and remove the background spines using Seaborn ?
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def sine_plot(flip=1):
x = np.linspace(0, 14, 99)
for i in range(1, 5):
plt.plot(x, np.sin(x + i * 0.59) * (11 - i) * flip)
# Set seaborn style
sns.set_style("white")
print("Plotting sine waves with clean axes")
sine_plot()
sns.despine()
plt.title("Sine Waves with Removed Spines")
plt.show()
Plotting sine waves with clean axes
Customizing despine() Parameters
The despine() function accepts several parameters to control which spines to remove ?
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Create sample data
x = np.linspace(0, 10, 50)
y = np.sin(x)
# Plot with different despine options
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Default despine (removes top and right)
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.title("Default despine()")
sns.despine()
# Remove all spines
plt.subplot(2, 2, 2)
plt.plot(x, y)
plt.title("Remove all spines")
sns.despine(top=True, right=True, left=True, bottom=True)
# Remove only left and bottom
plt.subplot(2, 2, 3)
plt.plot(x, y)
plt.title("Remove left and bottom")
sns.despine(left=True, bottom=True, top=False, right=False)
# Offset spines
plt.subplot(2, 2, 4)
plt.plot(x, y)
plt.title("Offset spines")
sns.despine(offset=10)
plt.tight_layout()
plt.show()
Parameters
| Parameter | Default | Description |
|---|---|---|
top |
True | Remove top spine |
right |
True | Remove right spine |
left |
False | Remove left spine |
bottom |
False | Remove bottom spine |
offset |
None | Distance to offset spines |
Practical Example with Real Data
Here's a more practical example using scatter plot data ?
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Generate sample data
np.random.seed(42)
x = np.random.normal(0, 1, 100)
y = 2 * x + np.random.normal(0, 0.5, 100)
# Create the plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, alpha=0.6)
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Scatter Plot with Clean Axes")
# Remove spines for cleaner look
sns.despine()
plt.show()
Conclusion
The despine() function in Seaborn provides a simple way to create cleaner, more professional-looking plots by removing unnecessary axis spines. Use it with different parameters to customize which spines to remove based on your visualization needs.
