Matplotlib allows you to plot beautiful figure for any dataset you want to analyze. But sometimes the labels on the x-axis are not readable. And due to it, you want to rotate the text. In this entire coding tutorial, you will know how to Rotate X axis labels in Matplotlib using the various examples.
Examples to Rotate X axis labels in Matplotlib
You should make sure that you do all the coding part involved here on your Jupyter Notebook. As I have used it for explaining the examples. It will be better for you for more understanding.
Examples 1: Rotate x axis labels on Simple Example
In this example, I will create sample data points for plotting the figures. After that, I will rotate the labels. Here I have used NumPy for the creation of the sample array. Execute the below lines of code.
import matplotlib.pyplot as plt import numpy as np %matplotlib inline x = np.array([1,2,3,4,5]) y = np.array([0.1,0.2,0.3,0.4,0.5]) plt.figure(figsize=(8,8)) labels =["AAAA","BBBB","CCCC","DDDD","EEEE","FFFF","GGGG","HHHH","IIII","JJJJ"] plt.plot(x,y) ax = plt.gca() ax.set_xticklabels(labels=labels,rotation=90);
Explanation of the Code
Here I used %matplotlib inline for showing the figure inline. Use it if you are coding in Jupyter Notebook. The x and y contain the NumPy array created using the numpy.array() method. Then use the figsize to change the size of the plot. After that instead of showing the x-axis points, I have created labels with the list of strings. It will be used to plot on the x-axis. After plotting the figure the function plt.gca() will get the current axis. And lastly to show the labels use ax.set_xticklabels(labels=labels,rotation=90) . Here 90 is the angle of labels you want to show. When you will run the above code you will get the output as below.
Example 2: Rotate X-axis labels in Matplotlib on Pandas Dataframe
The first example was very simple. Now, let’s plot and rotate labels on the dynamic dataset. For example, I have a forex pair dataset for the EURUSD pair. And I want to plot the line chart on the pair. If you simply plot the line chart then you will get the x-axis values randomly. But I want to get the date value instead of it. To do so you have to make the data column as the index value.
Default Figure without Rotation
Execute the below lines of code and see the output.
import matplotlib.pyplot as plt %matplotlib inline import pandas as pd data = pd.read_csv("EURUSD.csv") data.set_index("Time (UTC)",inplace=True) data.plot(figsize=(20,10))
You can see the data value is horizontally labeled. To rotate it you have to use the plt.xticks() method. Inside the method, you have to just pass the rotation value. For example, let’s pass the rotation=45 as an argument.
import matplotlib.pyplot as plt %matplotlib inline import pandas as pd data = pd.read_csv("EURUSD.csv") data.set_index("Time (UTC)",inplace=True) data.plot(figsize=(20,10)) plt.xticks(rotation=45)
You can see the x-axis labels have been rotated. In the same way, if you want the axis to be vertically labeled, then you will pass the rotation = 90.
In this entire tutorial, you have learned how to Rotate X axis labels in matplotlib. In the first method, you learned to rotate labels on the sample array. After that, you have learned how to implement the same on the CSV dataset using Pandas packages. Hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.