Rotate Tick Labels on X and Y Axes in Python Matplotlib

When I was working on a data visualization project for a U.S. sales dataset, I faced a common issue: the tick labels on my Matplotlib chart were overlapping. If you’ve ever plotted dates, city names, or long category names on your X or Y axis, you’ve probably run into this problem too.

At first glance, it seems like a small issue, but when you’re preparing charts for presentations or reports, readability matters a lot. That’s when I decided to explore different ways to rotate tick labels in Python Matplotlib, making my plots cleaner and more professional.

In this tutorial, I’ll show you two simple methods for rotating tick labels on both X and Y axes. These methods work for all types of Matplotlib charts, whether you’re plotting bar charts, line graphs, or scatter plots.

Rotate X-Axis Tick Labels in Python Matplotlib

When the tick labels on the X-axis are long (like U.S. city names or dates), they often overlap or become unreadable. Rotating them makes your chart much easier to interpret.

Below, I’ll show you two easy methods to rotate X-axis tick labels in Python Matplotlib.

Method 1 – Use plt.xticks(rotation=angle)

This is the simplest and most direct way to rotate tick labels on the X-axis. You just need to specify the rotation angle inside the plt.xticks() function.

Here’s how I use it in my Python projects:

import matplotlib.pyplot as plt

# Sample data: Average monthly temperature in major U.S. cities
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia']
temperature = [55, 68, 50, 72, 85, 58]

# Create a bar chart
plt.bar(cities, temperature, color='skyblue')

# Add labels and title
plt.title('Average Annual Temperature in Major U.S. Cities')
plt.xlabel('City')
plt.ylabel('Temperature (°F)')

# Rotate X-axis tick labels by 45 degrees
plt.xticks(rotation=45)

# Display the chart
plt.show()

You can see the output in the screenshot below.

Rotate Tick Labels on X and Y Axes Matplotlib

In this example, the city names are long, so rotating them by 45 degrees makes the chart much clearer. You can adjust the rotation angle (e.g., 30, 60, or 90) depending on your data.

Method 2 – Use ax.set_xticklabels()

When I need more control over the X-axis tick labels, such as changing font size, color, or alignment, I prefer using the object-oriented approach. This involves accessing the Axes object and modifying its properties directly.

Here’s how you can do it:

import matplotlib.pyplot as plt

# Sample data: U.S. state population (in millions)
states = ['California', 'Texas', 'Florida', 'New York', 'Illinois', 'Pennsylvania']
population = [39.0, 30.0, 22.0, 19.5, 12.8, 12.9]

fig, ax = plt.subplots(figsize=(8, 5))
ax.bar(states, population, color='lightgreen')

# Add labels and title
ax.set_title('Population of Top U.S. States (2025)')
ax.set_xlabel('State')
ax.set_ylabel('Population (Millions)')

# Rotate tick labels on X-axis
ax.set_xticklabels(states, rotation=30, ha='right', fontsize=10)

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Rotate Tick Labels on X and Y Axes in Python Matplotlib

In this example, I used ha=’right’ (horizontal alignment) to align the labels nicely with the ticks. This method is great when you’re customizing multiple properties of the X-axis labels in your Python Matplotlib plots.

Rotate Y-Axis Tick Labels in Python Matplotlib

Sometimes, the Y-axis tick labels are long, or numeric values overlap when you resize the figure or change the scale. Rotating them can make your chart more readable, especially for scientific or financial data.

Let’s look at two ways to rotate Y-axis tick labels in Python Matplotlib.

Method 1 – Use plt.yticks(rotation=angle)

Just like the plt.xticks() function, plt.yticks() allows you to rotate the Y-axis tick labels easily. This is the simplest way to adjust label orientation without dealing with the Axes object directly.

Here’s an example:

import matplotlib.pyplot as plt

# Sample data: Average monthly rainfall (in inches) for New York City
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
rainfall = [3.1, 2.9, 4.0, 4.5, 4.1, 4.3, 4.6, 4.4, 4.2, 3.8, 3.9, 3.7]

plt.plot(months, rainfall, marker='o', color='blue')

plt.title('Average Monthly Rainfall in New York City')
plt.xlabel('Month')
plt.ylabel('Rainfall (inches)')

# Rotate Y-axis tick labels by 45 degrees
plt.yticks(rotation=45)

plt.show()

You can see the output in the screenshot below.

Rotate Tick Labels on X and Y Axes Python Matplotlib

In this example, the Y-axis values are rotated by 45 degrees, which makes them easier to read when the chart is resized. This method is simple, fast, and works perfectly for most basic visualizations.

Method 2 – Use ax.tick_params() in Python

If you want full control over tick label appearance, including rotation, color, size, and direction, tick_params() is the most powerful option. I often use this method when I’m preparing charts for reports or dashboards.

Here’s how it works:

import matplotlib.pyplot as plt

# Sample data: U.S. GDP growth rate by year
years = [2018, 2019, 2020, 2021, 2022, 2023, 2024]
gdp_growth = [2.9, 2.3, -3.4, 5.7, 2.1, 2.5, 2.8]

fig, ax = plt.subplots(figsize=(7, 5))
ax.plot(years, gdp_growth, marker='o', color='orange', linewidth=2)

# Add labels and title
ax.set_title('U.S. GDP Growth Rate (2018–2024)')
ax.set_xlabel('Year')
ax.set_ylabel('GDP Growth (%)')

# Rotate Y-axis tick labels by 60 degrees
ax.tick_params(axis='y', labelrotation=60, labelsize=10, colors='darkred')

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Rotate Tick Labels on X and Y Axes Matplotlib

Here, I used labelrotation=60 to tilt the Y-axis labels and colors=’darkred’ to make them stand out. This approach gives you precise control over every aspect of tick label styling in Python Matplotlib.

Additional Tips for Rotating Tick Labels in Python Matplotlib

Over the years, I’ve learned a few best practices that make rotated tick labels look more professional:

  1. Use plt.tight_layout() – It automatically adjusts spacing so labels don’t get cut off.
  2. Keep rotation angles between 30° and 60° – These angles usually look best for text readability.
  3. Align labels properly – Use ha=’right’ or va=’center’ to align labels neatly.
  4. Avoid over-rotation – A 90° rotation should only be used when necessary.
  5. Use consistent fonts – This keeps your charts visually appealing across multiple plots.

These small adjustments can make a huge difference when creating professional visualizations for presentations or reports.

Common Use Cases

Here are a few real-world scenarios where rotating tick labels is especially helpful:

  • Finance: Displaying quarterly or monthly data for U.S. stock performance.
  • Retail: Showing product categories or store names in sales charts.
  • Weather Analysis: Plotting temperature or rainfall data by city and month.
  • Education: Comparing student scores or school performance across regions.

In all these cases, rotating tick labels makes your Python Matplotlib charts cleaner and easier to interpret.

Rotating tick labels might seem like a small detail, but it can dramatically improve the readability of your charts. Whether you’re working on a quick analysis or preparing a professional dashboard, clear labels make your data story stronger.

In this tutorial, I showed you four practical methods to rotate tick labels on both X and Y axes in Python Matplotlib: Each method has its own advantages; use the one that fits your project best.

You may also like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.