How to Rotate Tick Labels in Matplotlib (With Examples)


You can use the following syntax to rotate tick labels in Matplotlib plots:

#rotate x-axis tick labels
plt.xticks(rotation=45)

#rotate y-axis tick labels
plt.yticks(rotation=90)

The following examples show how to use this syntax in practice.

Example 1: Rotate X-Axis Tick Labels

The following code shows how to rotate the x-axis tick labels in Matplotlib:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

#create plot
plt.plot(x, y, color='red')

#rotate x-axis tick labels
plt.xticks(rotation=45)

Example 2: Rotate Y-Axis Tick Labels

The following code shows how to rotate the y-axis tick labels in Matplotlib:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

#create plot
plt.plot(x, y, color='blue')

#rotate y-axis tick labels
plt.yticks(rotation=90)

Example 3: Rotate Both Axes Tick Labels

The following code shows how to rotate the tick labels on both axes in Matplotlib:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

#create plot
plt.plot(x, y, color='green')

#rotate x-axis and y-axis tick labels
plt.xticks(rotation=45)
plt.yticks(rotation=90)

Additional Resources

How to Change the Number of Ticks in Matplotlib
How to Set Tick Labels Font Size in Matplotlib
How to Remove Ticks from Matplotlib Plots

Leave a Reply

Your email address will not be published. Required fields are marked *