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
Turn off the left/bottom axis tick marks in Matplotlib
To turn off the left or bottom axis tick marks in Matplotlib, we can use the tick_params() method with length=0 parameter. This removes the tick marks while keeping the labels visible.
Basic Syntax
plt.tick_params(axis='both', which='both', length=0)
Parameters
- axis − Specifies which axis to modify ('x', 'y', or 'both')
- which − Applies to 'major', 'minor', or 'both' ticks
- length − Sets the tick mark length (0 removes them)
Example: Remove All Tick Marks
Here's how to remove tick marks from both axes ?
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 10) y = np.sin(x) plt.plot(x, y) plt.tick_params(axis='both', which='both', length=0) plt.show()
Example: Remove Specific Axis Tick Marks
You can target specific axes by changing the axis parameter ?
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 4))
# Subplot 1: Remove only x-axis tick marks
plt.subplot(1, 2, 1)
x = np.linspace(0, 4*np.pi, 50)
y = np.cos(x)
plt.plot(x, y, 'b-')
plt.tick_params(axis='x', length=0)
plt.title('X-axis tick marks removed')
# Subplot 2: Remove only y-axis tick marks
plt.subplot(1, 2, 2)
plt.plot(x, y, 'r-')
plt.tick_params(axis='y', length=0)
plt.title('Y-axis tick marks removed')
plt.tight_layout()
plt.show()
Alternative Methods
You can also use gca() to get the current axes and modify tick parameters ?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 30)
y = np.sin(x)
plt.plot(x, y, 'go-', linewidth=2, markersize=4)
# Using gca() method
ax = plt.gca()
ax.tick_params(axis='both', which='both', length=0)
plt.title('Tick marks removed using gca()')
plt.grid(True, alpha=0.3)
plt.show()
Comparison
| Method | Syntax | Best For |
|---|---|---|
| Direct plt method | plt.tick_params() |
Simple plots |
| Using gca() | plt.gca().tick_params() |
More control over axes |
| Axes object | ax.tick_params() |
Subplot arrangements |
Conclusion
Use tick_params(length=0) to remove tick marks while preserving labels. Specify axis='x' or axis='y' to target specific axes, or use axis='both' for complete removal.
Advertisements
