How to rotate the Tick Labels in Matplotlib in Python

In this tutorial,  we will discuss how to rotate the tick labels in Matplotlib in Python.

Matplotlib:

Matplotlib is a Python library function. It is a graphical plotting library function in python. Matplotlib is a multi-platform data visualization library in 2D plots of an array in python which is numerically extended by NumPy array. It was founded by John Hunter in the year 2002.

NumPy:

NumPy is a Python array. Which stands for Numerical Python, which is a multidimensional array object. NumPy is Performed on the mathematical and logical operations in Python language.

Create a Plot:

Here, in python to print graphical plotting we need Matplotlib. pyplot library function. So we need to import Matplotlib library functions. We used mathematical operations in graphs so we need to import the NumPy library functions.

```#Program1
import matplotlib.pyplot as mat
import numpy as num

a= [0, 90, 180, 270, 360]
b = num.sin(a)

mat.plot(a,b)
mat.show()```

Rotate the Tick Labels in Matplotlib in Python

Now, let’s look at how to rotate the a and b axis. We use mat.xticks() and mat.yticks()  which are used to change the axis level in the graph plot. Here, both were used to change the axis individually.

```#Program2
import matplotlib.pyplot as mat
import numpy as num

a = [0, 90, 180, 270, 360]
b = num.sin(a)

mat.plot(a,b)
mat.xticks(rotation = 45)
mat.yticks(rotation = 45)
mat.show()
```
```#Progarm3
import matplotlib.pyplot as mat
import numpy as num

a = [0, 90, 180, 270, 360]
b = num.sin(a)

mat.plot(a,b)
ln = mat.gca()
ln.tick_params(axis='both', labelrotation = 90)
mat.show()```

In the above case, we use mat.gca() and tick_params() to rotate both axes at a time without individual statements.

Output:

1)#Program1

The above output graph is a normal plot before rotation.

2)#Program2

The above output graph shows that the rotation of the axis which are individual statements of code.

3)#Program3

The above graph shows that the rotation of the axis which is by a single statement of code.