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
Rotating axis text for each subplot in Matplotlib
When creating multiple subplots in Matplotlib, you may need to rotate axis labels to improve readability, especially when dealing with long tick labels or overlapping text. This can be achieved using xticks(), yticks(), or tick_params() methods.
Method 1: Using xticks() and yticks()
The simplest approach is to use plt.xticks(rotation=angle) and plt.yticks(rotation=angle) after creating each subplot ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.arange(1, 6)
y1 = x ** 2
y2 = x ** 3
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
# First subplot with rotated x-axis labels
ax1.plot(x, y1, 'b-o')
ax1.set_title('X-axis Labels Rotated 45°')
ax1.set_xlabel('Categories')
ax1.set_ylabel('Values')
ax1.set_xticklabels(['Category A', 'Category B', 'Category C', 'Category D', 'Category E'])
plt.sca(ax1) # Set current axis
plt.xticks(rotation=45)
# Second subplot with rotated y-axis labels
ax2.plot(x, y2, 'r-s')
ax2.set_title('Y-axis Labels Rotated 30°')
ax2.set_xlabel('X Values')
ax2.set_ylabel('Y Values')
plt.sca(ax2) # Set current axis
plt.yticks(rotation=30)
plt.tight_layout()
plt.show()
Method 2: Using tick_params()
A more direct approach is to use tick_params() method on each axis object ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
months = ['January', 'February', 'March', 'April', 'May']
sales_q1 = [100, 120, 140, 160, 180]
sales_q2 = [90, 110, 150, 170, 200]
# Create subplots
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
# Subplot 1: Rotate x-axis labels 45 degrees
axes[0, 0].bar(months, sales_q1, color='skyblue')
axes[0, 0].set_title('Q1 Sales - X Labels Rotated')
axes[0, 0].tick_params(axis='x', rotation=45)
# Subplot 2: Rotate x-axis labels 90 degrees
axes[0, 1].bar(months, sales_q2, color='lightgreen')
axes[0, 1].set_title('Q2 Sales - X Labels Vertical')
axes[0, 1].tick_params(axis='x', rotation=90)
# Subplot 3: Rotate both x and y axis labels
axes[1, 0].plot(months, sales_q1, 'o-', color='red')
axes[1, 0].set_title('Both Axes Rotated')
axes[1, 0].tick_params(axis='x', rotation=45)
axes[1, 0].tick_params(axis='y', rotation=30)
# Subplot 4: Different rotation angles
axes[1, 1].scatter(months, sales_q2, color='purple', s=100)
axes[1, 1].set_title('Custom Rotation Angles')
axes[1, 1].tick_params(axis='x', rotation=60)
plt.tight_layout()
plt.show()
Method 3: Using setp() for Multiple Subplots
For consistent rotation across multiple subplots, use plt.setp() ?
import matplotlib.pyplot as plt
import numpy as np
# Create data
categories = ['Product A', 'Product B', 'Product C', 'Product D']
values1 = [23, 45, 56, 78]
values2 = [12, 34, 67, 89]
values3 = [45, 23, 12, 56]
# Create subplots
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# Plot data
axes[0].bar(categories, values1, color='blue')
axes[0].set_title('Dataset 1')
axes[1].bar(categories, values2, color='green')
axes[1].set_title('Dataset 2')
axes[2].bar(categories, values3, color='red')
axes[2].set_title('Dataset 3')
# Rotate x-axis labels for all subplots
for ax in axes:
plt.setp(ax.get_xticklabels(), rotation=45, ha='right')
plt.tight_layout()
plt.show()
Comparison
| Method | Best For | Syntax |
|---|---|---|
xticks(rotation=) |
Individual subplots | plt.xticks(rotation=45) |
tick_params() |
More control over appearance | ax.tick_params(axis='x', rotation=45) |
setp() |
Multiple subplots at once | plt.setp(ax.get_xticklabels(), rotation=45) |
Conclusion
Use tick_params() for individual subplot control and setp() for consistent rotation across multiple subplots. Always include plt.tight_layout() to prevent overlapping labels.
Advertisements
