How can I rotate tick labels in Matplotlib? 2

How can I rotate tick labels in Matplotlib?

Rotating tick labels in Matplotlib allows users to adjust the angle of the labels on the x or y axis of a plot. This can be useful when dealing with long or overlapping labels, improving the readability and aesthetic of the plot. To rotate tick labels in Matplotlib, users can utilize the “rotation” parameter in the tick_params() function or the set_rotation() method. This feature provides flexibility and customization options for displaying tick labels in a visually appealing manner.

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.pyplotas 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.pyplotas 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.pyplotas 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)

Cite this article

stats writer (2024). How can I rotate tick labels in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rotate-tick-labels-in-matplotlib/

stats writer. "How can I rotate tick labels in Matplotlib?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-rotate-tick-labels-in-matplotlib/.

stats writer. "How can I rotate tick labels in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rotate-tick-labels-in-matplotlib/.

stats writer (2024) 'How can I rotate tick labels in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rotate-tick-labels-in-matplotlib/.

[1] stats writer, "How can I rotate tick labels in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I rotate tick labels in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top