How to Rotate X-Axis Tick Label Text in Matplotlib?

24 Jan, 2021
Matplotlib is an amazing and one of the most widely used data visualization library in Python for plots of arrays. It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It is much popular because of its customization options as we can tweak about any element from its hierarchy of objects.
To rotate X-axis labels, there are various methods provided by matplotlib i.e. change it on the Figure-level or by changing it on an Axes-level or individually by using built-in functions. Some methods are listed below :
Let’s create a simple line plot which we will modify in the further examples:
Python3
# Import librariesimport matplotlib.pyplot as pltimport numpy as np  # Creating datasetx = np.arange(0, np.pi*2, 0.05)y = np.sin(x**2)  # Creating plotplt.plot(x, y)  # Setting titleplt.title('Simple Example')  # Show plotplt.show()
Output :
Example 1: In this example, we will rotate X-axis labels on Figure-level using plt.xticks().
Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
ticks: This parameter is the list of xtick locations. and an optional parameter. If an empty list is passed as an argument then it will removes all xticks
labels: This parameter contains labels to place at the given ticks locations. And it is an optional parameter.
**kwargs: This parameter is Text properties that is used to control the appearance of the labels.
Python3
# Import librariesimport matplotlib.pyplot as pltimport numpy as np  # Creating datasetx = np.arange(0, np.pi*2, 0.05)y = np.sin(x**2)  # Creating plotplt.plot(x, y)  # Rotating X-axis labelsplt.xticks(rotation = 25)  # Setting titleplt.title('Rotating using plt.xticks()')  # Show plotplt.show()
Output :
Example 2: In this example, we will rotate X-axis labels on Axes-level using tick.set_rotation().
Syntax: Axes.get_xticks(self, minor=False)
Parameters: This method accepts the following parameters.
minor : This parameter is used whether set major ticks or to set minor ticks
Return value: This method returns a list of Text values.
Python3
# Import librariesimport matplotlib.pyplot as pltimport numpy as np  # Creating datasetx = np.arange(0, np.pi*2, 0.05)y = np.sin(x**2)  # Creating Figurefig, ax = plt.subplots()   # Creating plotax.plot(x, y)  # Rotating X-axis labelsax.set_xticklabels(ax.get_xticks(), rotation = 50)  # Setting titleplt.title('Rotating using tick.set_rotation()')  # Show plotplt.show()
Output :
Example 3: In this example, we will rotate X-axis labels individually by using built-in ax.set_xticklabels() function.
Python3
# Import librariesimport matplotlib.pyplot as pltimport numpy as np  # Creating datasetx = np.arange(0, np.pi*2, 0.05)y = np.sin(x**2)  # Creating Figurefig, ax = plt.subplots()   # Creating plotax.plot(x, y)  # Rotating X-axis labelsfor tick in ax.get_xticklabels():    tick.set_rotation(75)  # Setting titleplt.title('Rotating using ax.set_xticklabels()')  # Show plotplt.show()
Output :
Example 4: In this example, we will rotate X-axis labels individually by using built-in ax.xtick_params() function.
Syntax: matplotlib.pyplot.tick_params(axis=’both’, **kwargs)
Python3
# Import librariesimport matplotlib.pyplot as pltimport numpy as np  # Creating datasetx = np.arange(0, np.pi*2, 0.05)y = np.sin(x**2)  # Creating Figurefig, ax = plt.subplots()   # Creating plotax.plot(x, y)  # Rotating X-axis labelsax.tick_params(axis='x', labelrotation = 100)  # Setting titleplt.title('Rotating using ax.xtick_params()')  # Show plotplt.show()
Output :
Picked
Python-matplotlib
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Deque in Python
Defaultdict in Python
Queue in Python
Rotate axis tick labels in Seaborn and Matplotlib
Check if element exists in list in Python
Python Classes and Objects
Bar Plot in Matplotlib
Python OOPs Concepts
How To Convert Python Dictionary To JSON?