In this Python tutorial, we will discuss matplotlib rotate tick labels in python. And we will also cover the following topics:
- Matplotlib rotate tick labels
- Matplotlib rotate tick labels example
- Matplotlib rotate tick labels x-axis
- Matplotlib rotate tick labels y-axis
- Matplotlib rotate tick labels date
- Matplotlib rotate tick labels colorbar
- Matplotlib rotate tick labels 45 degrees
- Matplotlib rotate tick labels 90 degrees
- Matplotlib rotate tick labels alignment
Matplotlib rotate tick labels
- In python, matplotlib is one of the best libraries used for data visualization.
- Matplotlib library provides the functionality to customize the tick labels according to our choice.
- It provides the feature to rotate tick labels.
Firstly let’s understand what does ticks labels means:
- The markers representing data points on the axes are known as “Ticks“.
- And the name given to the markers is called “Ticks Labels“.
Matplotlib by default marks the data points on the axes, but it also provides us the feature to make settings of ticks and ticks label according to our choice.
In this section, we are going to learn about the rotation of tick labels.
The following steps are used to rotate tick labels in matplotlib which are outlined below:
- Defining Libraries: Import the important libraries which are required for the rotation of the tick labels (For visualization: pyplot from matplotlib, For data creation and manipulation: NumPy and Pandas).
- Define X and Y: Define the data values on X-axis and Y-axis.
- Plot a graph: By using the plot() method or any other method available for plotting you can plot the graph.
- Rotate tick labels: By using x.ticks() and y.ticks() method we can rotate tick labels.
- Display: At last display the plot by using the show() method.
The syntax to rotate tick labels is as below:
For X-axis labels
matplotlib.pyplot.xticks(ticks=None, labels=None, rotation=None, ......)
For Y-axis labels
matplotlib.pyplot.yticks(ticks=None, labels=None, rotation=None, ......)
The above-used parameters are outlined as below:
- ticks: It is an array-like structure. It specifies the list of xtick or ytick locations.
- labels: specifies labels to place at the given ticks locations. It is an array-like structure.
- rotation: specifies the angle of rotation.
Also, learn: How to install matplotlib python
Matplotlib rotate tick labels examples
In the above sections, we discussed what do we mean by tick labels and the syntax for rotation of tick labels.
Let’s understand the concept for rotation of tick labels with the help of an example as below:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color='red')
# Rotate tick labels
plt.xticks(rotation=30)
# Display graph
plt.show()
- In the above example, we import matplotlib. pyplot library. After this, we define data points of the x-axis and y-axis.
- plt.plot() method is used for the creation of the graph.
- plt.xticks() method is used for rotation of ticks labels at the x-axis. Here we pass the rotation parameter and set the rotation angle 30 degrees.
Conclusion! x-labels rotate at an angle of 30 degrees.
Read: Python plot multiple lines using Matplotlib
Matplotlib rotate tick labels x-axis
In this section, we will learn how to rotate X-axis tick labels.
There are two ways to rotate the x-axis:
- plt.xticks(): rotate on figure level.
- tick.set_rotation(): rotate on axes level.
- ax.set_xticklabels(): rotate on axes level.
- ax.tick_params()
Matplotlib rotate x-axis tick labels on figure level
For rotation of tick labels on figure level, firstly we have to plot the graph by using the plt.plot() method.
After this, we have to call plt.xticks() method and pass the rotation argument and set their value as per your choice.
The syntax to change the rotation of x-axis ticks on figure level is as below:
matplotlib.pyplot.xticks(rotation=)
The above-used parameters are described below:
- rotation: to set angel for rotation.
Let’s see how rotation on figure level works with an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color='red')
# Rotate tick labels
plt.xticks(rotation=175)
# Display graph
plt.show()
In the above example, we plot the graph by using plt. plot() method and after that, we call plt.xticks() method and set the angle of rotation at 175 degrees.
Matplotlib rotate x-axis tick labels on axes level
For rotation of tick labels on figure level, firstly we have to plot the graph by using the plt.draw() method.
After this, you have to call the tick.set_rotation() method and pass the rotation angle value as an argument.
The syntax to change the rotation of x-axis ticks on axes level is as below:
matplotlib.pyplot.set_ticks(rotation angle)
The above-used parameters are described below:
- rotation angle: to set angel for rotation to move x-axis labels.
Let’s see how rotation on axes level works with an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color = 'm')
ax = plt.gca()
# Call draw function
plt.draw()
# Tick rotation on axes
for tick in ax.get_xticklabels():
tick.set_rotation(63)
# Display graph
plt.show()
In the above example, firstly we call the draw() method and after this use for loop and tick.set_rotation() method and set angle of rotation 63 degree.
Matplotlib rotate x axis tick labels by using ax.set_xticklabels() method
Another way to rotate X-axis tick labels is using the ax.set_xticklabels() method. Before this, you have to get the current axes of the object.
Remember before calling this method you’ll have to call plt.draw() method.
The syntax for the above method is given below:
ax.set_xticklabels(ax.get_xticks(), rotation =)
Let’s understand the concept with the help of an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color = 'orange')
ax = plt.gca()
# Call draw function
plt.draw()
# Tick rotation on axes
ax.set_xticklabels(ax.get_xticks(), rotation = 10)
# Display graph
plt.show()
In the above example, firstly we call the plt.draw() method, and then we ax.set_xticklabels() method and set rotation angle to 10 degrees.
Matplotlib rotate x-axis tick labels by using ax.tick_parmas()
Another way to rotate x-axis tick labels is using the ax.tick_parmas() method. Before this, you have to get the current axes of the object.
The syntax for this method is given below:
ax.tick_params(axis=None, labelrotation= None)
The above-used arguments are outlined below:
- axis: specifies the axis in which you want rotation.
- labelrotation: specifics angle of rotation.
Let’s understand the concept with the help of an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Crate Plot
plt.plot(x, y, color = 'orange')
ax = plt.gca()
# Rotate x-axis labels
ax.tick_params(axis='x', labelrotation = 45)
# Display graph
plt.show()
In the above example, we use the ax.tick_params() method and pass “axis” as argument and set their value to be “x” and also pass “labelrotation” as argument and set their value to be 45.
Read: Matplotlib plot a line
Matplotlib rotate tick labels Y-axis
In this section, we will learn how to rotate Y-axis tick labels.
There are two ways to rotate the y-axis:
- plt.yticks(): rotate on figure level.
- tick.set_rotation(): rotate on axes level.
- ax.set_yticklabels(): rotate on axes level.
- ax.tick_params()
Matplotlib rotate y-axis tick labels on figure level
For rotation of tick labels on figure level, firstly we have to plot the graph by using the plt.plot() method.
After this, we have to call plt.yticks() method and pass the rotation argument and set their value as per your choice.
The syntax to change the rotation of y-axis ticks on figure level is as below:
matplotlib.pyplot.yticks(rotation=)
The above-used parameters are described below:
- rotation: to set angel for rotation.
Let’s see how rotation on figure level works with an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y)
# Rotate tick labels
plt.yticks(rotation=63)
# Display graph
plt.show()
In the above example, we plot the graph by using plt. plot() method and after that, we call plt.yticks() method and set the angle of rotation at 63 degrees.
Conclusion! Y-axis tick labels rotate at an angle of 63 degree.
Read: What is matplotlib inline
Matplotlib rotate y-axis tick labels on axes level
For rotation of tick labels on figure level firstly you have to plot the graph by using the plt.draw() method.
After this, you have to call the tick.set_rotation() method and pass the rotation angle value as an argument.
The syntax to change the rotation of y-axis ticks on axes level is as below:
matplotlib.pyplot.set_ticks(rotation angle)
The above-used parameters are described below:
- rotation angle: to set angel for rotation to move y-axis labels.
Let’s see how rotation on axes level works with an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color = 'm')
ax = plt.gca()
# Call draw function
plt.draw()
# Tick rotation on axes
for tick in ax.get_yticklabels():
tick.set_rotation(63)
# Display graph
plt.show()
In the above example, firstly we call the draw() method and after this use for loop and tick.set_rotation() method and set angle of rotation 23 degree.
Conclusion! Y-axis labels rotate at an angle of 23 degrees.
Matplotlib rotate Y axis tick labels by using ax.set_yticklabels() method
Another way to rotate Y-axis tick labels is using the ax.set_yticklabels() method. Before this, you have to get the current axes of the object.
Remember before calling this method you’ll have to call plt.draw() method.
The syntax for the above method is given below:
ax.set_yticklabels(ax.get_yticks(), rotation =)
Let’s understand the concept with the help of an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Create Plot
plt.plot(x, y, color = 'orange')
ax = plt.gca()
# Call draw function
plt.draw()
# Tick rotation on axes
ax.set_yticklabels(ax.get_yticks(), rotation = 10)
# Display graph
plt.show()
In the above example, firstly we call the plt.draw() method, and then we ax.set_yticklabels() method and set rotation angle to 10 degrees.
Matplotlib rotate Y-axis tick labels by using ax.tick_parmas()
Another way to rotate Y-axis tick labels is using the ax.tick_parmas() method. Before this, you have to get the current axes of the object.
The syntax for this method is given below:
ax.tick_params(axis=None, labelrotation= None)
The above-used arguments are outlined below:
- axis: specifies the axis in which you want rotation.
- labelrotation: specifics angle of rotation.
Let’s understand the concept with the help of an example:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [7, 14, 21, 28]
# Crate Plot
plt.plot(x, y, color = 'orange')
ax = plt.gca()
# Rotate y-axis labels
ax.tick_params(axis='y', labelrotation = 45)
# Display graph
plt.show()
In the above example, we use the ax.tick_params() method and pass “axis” as argument and set their value to be “y” and also pass “labelrotation” as argument and set their value to be 45.
Read: Matplotlib plot bar chart
Matplotlib rotate tick labels date
The reason behind the rotation of the tick labels is overlapping. Mostly, the date ticks are long in size and start to overlap. To avoid this condition we rotate the date tick labels.
- To avoid the overlapping of dates on the x-axis we use the fig.autofmt_xdate() method.
- This method automatically set the rotation of dates and tune the x-axis or you can also set the rotation angle of yours choice.
The syntax for rotation of dates on x-axis:
matplotlib.figure.Figure.autofmt_xdate(bottom=0.2,roation=10,ha='left',which=None)
The following parameters are used in the above function which is outlined below:
- bottom: specifies the bottom of the plot.
- rotation: specifies rotation angle.
- ha: specifics horizontal alignment.
- which: specifies which tickable to rotate.
Let’s understand the concept with the help of an example:
Code#1
Here is code without the autofmt_xdate() method.
# Import Libraries
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
# Define Dates
dates = [
datetime(2021, 10, 21),
datetime(2021, 7, 24),
datetime(2021, 8, 25),
datetime(2021, 10, 26),
]
y = [2, 4, 4.5, 6]
# Plot Dates
fig = plt.figure()
plt.plot_date(dates, y, linestyle= 'dashed')
# Display Graph
plt.show()
In the above code, we simply create data consist of dates on the x-axis and plot them.
Conclusion! The problem of overlapping caused. To remove this follow Code#2
Code#2
# Import Libraries
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
# Define Dates
dates = [
datetime(2021, 10, 21),
datetime(2021, 7, 24),
datetime(2021, 8, 25),
datetime(2021, 10, 26),
]
y = [2, 4, 4.5, 6]
# Plot Dates
fig = plt.figure()
plt.plot_date(dates, y, linestyle= 'dashed')
# Rotate dates on x-xis
fig.autofmt_xdate()
# Display Graph
plt.show()
In the above code, we create data consist of dates on the x-axis, and then we use the autofmt_xdate() method to avoid overlapping or to rotate dates on the x-axis.
Conclusion! The problem of overlapping resolve by rotating the x-axis.
Read: Matplotlib subplot tutorial
Matplotlib rotate tick labels subplot
Sometimes we have multiple subplots in a figure area. And we want to customize only one subplot axis or rotate the axis of a specific subplot.
In that case, we use the method set_xticklabels() for rotation of the axis.
The syntax for this is given below:
matplotlib.axis.Axis.set_xticlabels(labels=, rotation=)
The above-used parameters are described below:
- labels: set labels for rotation.
- rotation: specifies the angle of rotation.
Let’s clear the concept by example:
# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
# Define Data
x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]
x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]
x3= [5, 8, 12]
y3= [3, 6, 9]
x4= [7, 8, 15]
y4= [6, 12, 18]
# Plot graph
fig, ax = plt.subplots(2, 2)
ax[0, 0].set_facecolor('cyan')
# Set axis for rotation
ax[0,0].set_xticklabels([0.2, 0.4, 0.6, 0.8, 1], rotation = 30)
ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)
# Display graph
fig.tight_layout()
plt.show()
In the above example, we use the set_xticklabels() method to rotate the x-axis of 1st subplot at 30 degrees angle rotation.
Read: Matplotlib best fit line
Matplotlib rotate tick label colorbar
The Colorbar is the map of data values of colors. The colorbar() method is used to add a colorbar to the graph.
If we want to rotate axes of the color bar for better visualization we have two methods as follow:
- cbar.ax.set_xticklabels: if the orientation of colorbar is horizontal.
- cbar.ax.set_yticklabels: if the orientation of colorbar is vertical.
Matplotlib rotate tick label colobar horizontal
Here, we will learn how to rotate colorbar axis of the bar horizontal placed.
The syntax for this is given below:
cbar.ax.set_xticklabels(labels, rotation)
The parameters used above are:
- labels: specifies labels of colorbar
- rotation: specifies the angle of rotation
Let’s take an example to understand how to do rotation:
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((5, 5))
plt.imshow(a, cmap='summer')
# Plot horizontal colorbar
cbar = plt.colorbar(
orientation="horizontal")
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate x tick labels
cbar.ax.set_xticklabels(labels, rotation=40)
#Plot graph
plt.show()
- In the above example, we import library matplotlib and numpy. Then we plot data using numpy.
- After that, we set the orientation to “horizontal” and use the set_xticklabels() method to set the rotation angle to 40 degrees.
Matplotlib rotate tick label colobar vertical
Here, we will learn how to rotate colorbar axis of the bar vertically placed.
The syntax for this is given below:
cbar.ax.set_yticklabels(labels, rotation)
The parameters used above are:
- labels: specifies labels of colorbar
- rotation: specifies the angle of rotation
Let’s take an example to understand how to do rotation:
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((5, 5))
plt.imshow(a, cmap='summer')
# Plot vertical colorbar
cbar = plt.colorbar(
orientation="vertical")
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate y tick labels
cbar.ax.set_yticklabels(labels, rotation=40)
# Plot graph
plt.show()
- In the above example, we import library matplotlib and numpy. Then we plot data using numpy.
- After that, we set the orientation to “vertical” and use the set_yticklabels() method to set the rotation angle to 40 degrees.
Read: Matplotlib subplots_adjust
Matplotlib rotate tick labels 45 degrees
In this section, we are going to study how we rotate X-ticks and Y-ticks in a plotted at a specific angle of 45 degrees.
The main reason behind the rotation of ticks is to avoid overlapping and for a clear view of the graph axis.
Here we will study three cases of rotation of tick labels at 45 degrees.
- 1st: We study how to rotate X-axis tick labels at 45 degrees.
- 2nd: We study how to rotate Y-axis tick labels at 45 degrees.
- 3rd: We study how to rotate X-axis and Y-axis tick labels at 45 degrees at a time.
The syntax to rotate tick labels at 45 degrees is as below:
Matplotlib.pyplot.xticks(rotation = 45)
Let’s understand the concept more clearly with the help of an example:
Case#1 (Rotation of X tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [1, 2, 3, 4]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y, color='orange')
# Rotate X-axis tick labels
plt.xticks(rotation=45)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation at 45 degrees by using plt.xticks() method and set rotation= 45 as the argument in the method.
- Finally, display the figure by using the plt.show() method.
Case#2 (Rotation of Y tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [1, 2, 3, 4]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y, color='orange')
# Rotate Y-axis tick labels
plt.yticks(rotation=45)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation at 45 degrees by using plt.yticks() method and set rotation= 45 as the argument in the method.
- Finally, display the figure by using the plt.show() method.
Case#3 (Rotation of both tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [1, 2, 3, 4]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y, color='orange')
# Rotate X-axis and Y-axis tick labels
plt.xticks(rotation=45)
plt.yticks(rotation=45)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks and yticks label rotation at 45 degrees by using plt.xticks() and plt.yticks() method for the x-axis and y-axis respectively and set rotation= 45 as the argument in the method.
- In last, display the figure by using the plt.show() method.
Read: Matplotlib log log plot
Matplotlib rotate tick labels 90 degrees
In this section, we are going to study how we rotate X-ticks and Y-ticks in a plotted at a specific angle of 90 degrees.
The main reason behind the rotation of ticks is to avoid overlapping and for a clear view of the graph axis.
Here we study three cases of rotation of tick labels at 90 degrees.
- 1st: We study how to rotate X-axis tick labels at 90 degrees.
- 2nd: We study how to rotate Y-axis tick labels at 90 degrees.
- 3rd: We study how to rotate X-axis and Y-axis tick labels at 90 degrees at a time.
The syntax to rotate tick labels at 90 degrees is as below:
Matplotlib.pyplot.xticks(rotation = 90)
Let’s understand the concept more clearly with the help of an example:
Case#1 (Rotation of X tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [5, 6, 7, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Rotate X-axis tick labels
plt.xticks(rotation=90)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation at 90 degrees by using plt.xticks() method and set rotation= 90 as the argument in the method.
- Finally, display the figure by using the plt.show() method.
Case#2 (Rotation of Y tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [5, 6, 7, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Rotate Y-axis tick labels
plt.yticks(rotation=90)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation at 90 degrees by using plt.yticks() method and set rotation= 90 as the argument in the method.
- Finally, display the figure by using the plt.show() method.
Case#3 (Rotation of both tick labels)
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [5, 6, 7, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Rotate X-axis and Y-axis tick labels
plt.xticks(rotation=90)
plt.yticks(rotation=90)
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks and yticks label rotation at 90 degrees by using plt.xticks() and plt.yticks() method for the x-axis and y-axis respectively and set rotation= 90 as the argument in the method.
- In last, display the figure by using the plt.show() method.
Read: Matplotlib plot_date
Matplotlib rotate tick labels alignment
In this section, we are going to study how to align tick labels. We can say that how we will arrange tick labels in different positions.
For alignment of tick labels, we use the ‘ha’ argument which means “Horizontal alignment” and we pass this argument to xticks() and yticks() method.
We can align tick labels in the following positions as given below:
- ha=’right’: specifies that tick labels align at the right end.
- ha=’center’: specifies that tick labels align at the center.
- ha=’left’: specifies that tick labels align at the left end.
Let’s understand each alignment case properly:
Matplotlib rotate tick label alignment right
We are going to study how to align tick labels at the right end.
The syntax for right alignment is as given below:
# For x-axis
matplotlib.pyplot.xticks(ha='right')
# For y-axis
matplotlib.pyplot.yticks(ha='right')
Let’s see an example related to right alignment:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Right align X-axis tick labels
plt.xticks(ha='right')
# OR
# Right align Y-axis tick labels
plt.yticks(ha='right')
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation alignemnt at right end by using plt.xticks() with parameter ha=’right’.
- And If you want to aligne yaxis tick label use the method plt.yticks() and pass the parameter ha=’right’.
- In last, display the figure by using the plt.show() method.
Matplotlib rotate tick label alignment center
We are going to study how to align tick labels at the center.
The syntax for center alignment is as given below:
# For x-axis label
matplotlib.pyplot.xticks(ha='center')
# For y-axis label
matplotlib.pyplot.yticks(ha='center')
Let’s see an example related to center alignment:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Center align X-axis tick labels
plt.xticks(ha='center')
# OR
# Center align Y-axis tick labels
plt.yticks(ha='center')
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation alignemnt at center by using plt.xticks() with parameter ha=’center’.
- And If you want to aligne yaxis tick label use the method plt.yticks() and pass the parameter ha=’center’.
- In last, display the figure by using the plt.show() method.
Matplotlib rotate tick label alignment left
We are going to study how to align tick labels at the left end.
The syntax for left alignment is as given below:
# For x-axis label
matplotlib.pyplot.xticks(ha='left')
# For y-axis label
matplotlib.pyplot.yticks(ha='left')
Let’s see an example related to left alignment:
# Import Libraries
import matplotlib.pyplot as plt
# Define data
x = [2, 4, 6, 8]
y = [8, 16, 20, 12]
# Create plot
plt.plot(x, y)
# Left align X-axis tick labels
plt.xticks(ha='left')
# OR
# Left align Y-axis tick labels
plt.yticks(ha='left')
# Display the Graph
plt.show()
- Import the library matplotlib.pyplot for visualization of data.
- Define data the X-axis and Y-axis and create a plot by using the plt.plot() method.
- Set xticks label rotation alignemnt at left end by using plt.xticks() with parameter ha=’left’.
- And If you want to aligne yaxis tick label use the method plt.yticks() and pass the parameter ha=’left’.
- In last, display the figure by using the plt.show() method.
You may like to read more on Matplotlib.
In this Python tutorial, we have discussed the “Matplotlib rotate tick labels” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.
- Matplotlib rotate tick labels
- Matplotlib rotate tick labels example
- Matplotlib rotate tick labels x-axis
- Matplotlib rotate tick labels y-axis
- Matplotlib rotate tick labels date
- Matplotlib rotate tick labels colorbar
- Matplotlib rotate tick labels 45 degrees
- Matplotlib rotate tick labels 90 degrees
- Matplotlib rotate tick labels alignment
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.