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
Selected Reading
How to remove or hide X-axis labels from a Seaborn / Matplotlib plot?
To remove or hide X-axis labels from a Seaborn / Matplotlib plot, you can use several methods. The most common approach is using set(xlabel=None) on the axes object.
Using set(xlabel=None)
This method removes the X-axis label while keeping the tick labels ?
import matplotlib.pyplot as plt
import seaborn as sns
# Set figure size
plt.rcParams["figure.figsize"] = [8, 4]
plt.rcParams["figure.autolayout"] = True
# Set Seaborn style
sns.set_style("whitegrid")
# Load example dataset
tips = sns.load_dataset("tips")
# Create boxplot and remove X-axis label
ax = sns.boxplot(x="day", y="total_bill", data=tips)
ax.set(xlabel=None)
plt.show()
Using set_xlabel("")
Alternative method to set an empty string as the label ?
import matplotlib.pyplot as plt
import seaborn as sns
# Create sample data
tips = sns.load_dataset("tips")
# Create plot and remove X-axis label
plt.figure(figsize=(8, 4))
ax = sns.barplot(x="day", y="total_bill", data=tips)
ax.set_xlabel("")
plt.show()
Removing Both Labels and Tick Labels
To completely hide X-axis labels and tick labels ?
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
plt.figure(figsize=(8, 4))
ax = sns.scatterplot(x="total_bill", y="tip", data=tips)
# Remove X-axis label and tick labels
ax.set(xlabel=None)
ax.set_xticklabels([])
plt.show()
Methods Comparison
| Method | X-axis Label | Tick Labels | Use Case |
|---|---|---|---|
set(xlabel=None) |
Hidden | Visible | Keep tick labels, hide axis label |
set_xlabel("") |
Hidden | Visible | Alternative to xlabel=None |
set_xticklabels([]) |
Visible | Hidden | Hide tick labels only |
| Both methods | Hidden | Hidden | Completely clean X-axis |
Conclusion
Use ax.set(xlabel=None) to remove X-axis labels while keeping tick labels visible. For complete X-axis cleanup, combine it with set_xticklabels([]) to hide both labels and tick marks.
Advertisements
