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 make longer subplot tick marks in Matplotlib?
To make longer subplot tick marks in Matplotlib, we can use the tick_params() method to control the length and width of both major and minor ticks.
Steps
Create a subplot using subplot() method
Plot data points to visualize the tick marks
Enable minor ticks using minorticks_on()
Use tick_params() to customize tick appearance with length and width parameters
Display the figure using show() method
Basic Example
Here's how to create longer tick marks on a subplot ?
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create subplot
ax1 = plt.subplot()
# Plot simple data
ax1.plot(range(5), [x**2 for x in range(5)], linewidth=2, marker='o')
# Enable minor ticks
ax1.minorticks_on()
# Customize major ticks (longer and thicker)
ax1.tick_params('both', length=20, width=2, which='major')
# Customize minor ticks (shorter but visible)
ax1.tick_params('both', length=10, width=1, which='minor')
plt.title('Subplot with Custom Tick Marks')
plt.show()
Parameters Explained
| Parameter | Description | Example Values |
|---|---|---|
axis |
Which axis to apply changes ('x', 'y', 'both') | 'both', 'x', 'y' |
length |
Length of tick marks in points | 10, 20, 30 |
width |
Width of tick marks in points | 1, 2, 3 |
which |
Apply to major or minor ticks | 'major', 'minor', 'both' |
Customizing Individual Axes
You can also customize x and y axes separately ?
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots(figsize=(8, 6))
# Plot data
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
ax.plot(x, y, 'b-o', linewidth=2)
# Enable minor ticks
ax.minorticks_on()
# Customize x-axis ticks (longer)
ax.tick_params(axis='x', length=25, width=3, which='major')
ax.tick_params(axis='x', length=12, width=1, which='minor')
# Customize y-axis ticks (shorter)
ax.tick_params(axis='y', length=15, width=2, which='major')
ax.tick_params(axis='y', length=8, width=1, which='minor')
ax.set_title('Different Tick Lengths for X and Y Axes')
plt.show()
Conclusion
Use tick_params() with length and width parameters to create longer tick marks. Enable minor ticks with minorticks_on() for better plot readability and professional appearance.
Advertisements
