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 to specify values on Y-axis in Python Matplotlib?
In Matplotlib, you can customize the Y-axis values using the yticks() method to specify both the positions and labels of the ticks. This is useful when you want to replace numeric values with descriptive labels or control exactly which values appear on the axis.
Basic Y-axis Customization
The yticks() method allows you to specify tick positions and their corresponding labels −
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
x = np.array([0, 2, 4, 6])
y = np.array([1, 3, 5, 7])
# Create custom labels for both axes
ticks = ['Low', 'Medium', 'High', 'Very High']
# Set custom ticks and labels
plt.xticks(x, ticks)
plt.yticks(y, ticks)
# Plot the data
plt.plot(x, y, color='green', marker='o')
plt.title('Custom Y-axis Values')
plt.show()
Specifying Only Y-axis Values
You can customize only the Y-axis while keeping the X-axis default −
import numpy as np
import matplotlib.pyplot as plt
# Create data points
x = np.linspace(0, 10, 20)
y = x ** 2
# Define custom Y-axis positions and labels
y_positions = [0, 25, 50, 75, 100]
y_labels = ['Zero', 'Quarter', 'Half', 'Three-Quarter', 'Full']
plt.plot(x, y, 'b-')
plt.yticks(y_positions, y_labels)
plt.xlabel('X Values')
plt.ylabel('Custom Y Labels')
plt.title('Custom Y-axis Labels Only')
plt.grid(True, alpha=0.3)
plt.show()
Different Approaches
| Method | Usage | Best For |
|---|---|---|
plt.yticks(positions, labels) |
Set both positions and labels | Custom categorical labels |
plt.yticks(positions) |
Set only positions | Controlling tick density |
ax.set_yticks() |
Object-oriented approach | Multiple subplots |
Advanced Example with Multiple Customizations
Here's a comprehensive example showing various Y-axis customization options −
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
months = np.arange(1, 13)
sales = np.array([20, 35, 30, 35, 27, 40, 45, 38, 42, 48, 35, 30])
# Create the plot
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the data
ax.plot(months, sales, 'ro-', linewidth=2, markersize=6)
# Customize Y-axis
y_ticks = [20, 30, 40, 50]
y_labels = ['Poor\n(20)', 'Average\n(30)', 'Good\n(40)', 'Excellent\n(50)']
ax.set_yticks(y_ticks)
ax.set_yticklabels(y_labels)
# Customize X-axis
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
ax.set_xticks(months)
ax.set_xticklabels(month_names)
# Add labels and title
ax.set_xlabel('Month')
ax.set_ylabel('Sales Performance')
ax.set_title('Monthly Sales with Custom Y-axis Labels')
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Conclusion
Use plt.yticks() to customize Y-axis values with specific positions and labels. This method is essential for creating meaningful categorical labels and controlling exactly which values appear on your axis. For object-oriented plotting, use ax.set_yticks() and ax.set_yticklabels() for more control.
