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
Hiding major tick labels while showing minor tick labels in Matplotlib
To hide major tick labels while showing minor tick labels in Matplotlib, you can use the setp() method to control the visibility of specific tick label types. This is useful when you want a cleaner plot appearance with less cluttered labels.
Basic Example
Here's how to hide major tick labels while keeping minor ones visible ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.figure(figsize=(8, 4))
# Create data
x = np.linspace(1, 10, 100)
y = np.log(x)
# Plot the data
plt.plot(x, y, 'b-', linewidth=2)
# Hide major tick labels
plt.setp(plt.gca().get_xmajorticklabels(), visible=False)
# Show minor ticks
plt.gca().minorticks_on()
plt.title('Logarithmic Function with Hidden Major Labels')
plt.grid(True, alpha=0.3)
plt.show()
Complete Example with Both Axes
You can also control tick labels on both x and y axes ?
import matplotlib.pyplot as plt
import numpy as np
# Create figure
plt.figure(figsize=(8, 5))
# Generate sample data
x = np.linspace(0, 4*np.pi, 200)
y = np.sin(x) * np.exp(-x/8)
plt.plot(x, y, 'r-', linewidth=2, label='Damped Sine Wave')
# Enable minor ticks
ax = plt.gca()
ax.minorticks_on()
# Hide major tick labels on x-axis
plt.setp(ax.get_xmajorticklabels(), visible=False)
# Hide major tick labels on y-axis
plt.setp(ax.get_ymajorticklabels(), visible=False)
# Customize minor tick appearance
ax.tick_params(which='minor', length=4, color='gray')
ax.tick_params(which='major', length=6, color='black')
plt.title('Plot with Only Minor Tick Labels Visible')
plt.legend()
plt.grid(True, which='both', alpha=0.3)
plt.show()
Alternative Method Using tick_params()
You can achieve the same result using tick_params() method ?
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(8, 4))
x = np.linspace(0, 10, 50)
y = x**2
plt.plot(x, y, 'g-', marker='o', markersize=4)
# Alternative method to hide major tick labels
ax = plt.gca()
ax.tick_params(axis='x', which='major', labelbottom=False)
ax.tick_params(axis='y', which='major', labelleft=False)
# Enable minor ticks
ax.minorticks_on()
plt.title('Quadratic Function - Minor Ticks Only')
plt.grid(True, alpha=0.3)
plt.show()
Key Methods Summary
| Method | Syntax | Use Case |
|---|---|---|
setp() |
plt.setp(ax.get_xmajorticklabels(), visible=False) |
Direct control over tick label visibility |
tick_params() |
ax.tick_params(which='major', labelbottom=False) |
More comprehensive tick customization |
minorticks_on() |
ax.minorticks_on() |
Enable minor ticks display |
Conclusion
Use setp() with get_xmajorticklabels() to hide major tick labels while keeping minor ones visible. The tick_params() method provides more granular control over tick appearance and is often preferred for complex customizations.
Advertisements
