6.1 C
London
Thursday, December 19, 2024
HomePythonMatplotlib in PythonHow to Rotate Tick Labels in Matplotlib (With Examples)

How to Rotate Tick Labels in Matplotlib (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.pyplot as 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.pyplot as 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.pyplot as 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)

Additional Resources

How to Change the Number of Ticks in Matplotlib
How to Set Tick Labels Font Size in Matplotlib
How to Remove Ticks from Matplotlib Plots

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories