How to Rotate X-Axis Tick Label Text in Matplotlib?
Last Updated :
16 Jun, 2025
Rotating X-axis tick labels in Matplotlib improves readability, especially when labels are long or overlap. Angles like 45° or 90° help display dates or categories clearly. Matplotlib provides several easy ways to rotate labels at both the figure and axes levels. Let’s explore the most effective ones.
Using ax.tick_params()
This method belongs to the Axes object in Matplotlib and allows you to rotate X-axis tick labels using the label rotation parameter. It's a clean, efficient and scalable solution especially suited for complex figures with multiple subplots.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x', labelrotation=45)
plt.title('Using ax.tick_params()')
plt.show()
Output
Explanation: A plot is created with plt.subplots() and ax.plot(x, y), X-axis labels are rotated 45° using ax.tick_params() for better readability and the plot is shown with plt.show().
Using plt.xticks(rotation=...)
This is a Figure-level function from matplotlib.pyplot that sets or rotates the tick labels on the X-axis. By passing the rotation argument, you can easily angle the labels without any additional complexity.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
plt.plot(x, y)
plt.xticks(rotation=45)
plt.title('Using plt.xticks()')
plt.show()
Output
Explanation: A line plot of y = sin(x²) from 0 to 2π is created with plt.plot(). X-axis labels are rotated 45° using plt.xticks(), a title is set and the plot is displayed with plt.show().
Using tick.set_rotation()
Here, you loop through each X-axis label retrieved using ax.get_xticklabels() and manually apply rotation using tick.set_rotation(angle). This gives fine-grained control over each tick label.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
# Individual tick rotation
for tick in ax.get_xticklabels():
tick.set_rotation(45)
plt.title('Using tick.set_rotation()')
plt.show()
Output
Explanation: A plot of y = sin(x²) is created using ax.plot(). The X-axis tick labels are individually accessed using ax.get_xticklabels() and rotated 45° using tick.set_rotation(45).
Using ax.set_xticklabels()
This method sets the tick labels explicitly on the X-axis and allows you to rotate them as well. It’s part of the Axes class and replaces the existing tick labels with the ones you provide or get from get_xticks().
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticklabels(ax.get_xticks(), rotation=45)
plt.title('Using ax.set_xticklabels()')
plt.show()
Output
Explanation: A plot of y = sin(x²) is created, and X-axis labels are rotated 45° using ax.set_xticklabels(). This method offers label formatting control but is best used with ax.set_xticks() to ensure proper alignment.
Related articles