How to Rotate Tick Labels in Matplotlib

Working with data visualization in Python, I often find myself needing to make charts not only informative but also easy to read. One common issue I face is overlapping or cluttered tick labels on the axes, especially when dealing with long text like city names or dates.

Matplotlib, the go-to plotting library in Python, does not automatically handle this well. Luckily, rotating tick labels is a simple fix that instantly improves the clarity of any plot.

In this article, I’ll share my experience and show you multiple ways to rotate tick labels in Matplotlib.

Methods to Rotate Tick Labels in Matplotlib

Imagine you’re plotting the monthly sales figures for major U.S. retail chains. The x-axis labels are the store names, which can be long and cause overlapping when displayed horizontally.

Rotating these labels makes them easier to read, prevents overlap, and enhances the overall look of your chart. It’s a small tweak, but it makes a significant difference in how your audience interprets your data.

Read Matplotlib Subplot Tutorial

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

The simplest way to rotate tick labels is by using the rotation parameter in plt.xticks().

Here’s an example where I plot quarterly sales figures for five U.S. cities:

import matplotlib.pyplot as plt

cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
sales = [250, 200, 150, 175, 125]

plt.bar(cities, sales)
plt.xticks(rotation=45)  # Rotate x-axis labels by 45 degrees
plt.title('Quarterly Sales by City')
plt.xlabel('City')
plt.ylabel('Sales (in thousands)')
plt.show()

I executed the above example code and added the screenshot below.

rotate xticks matplotlib

It’s quick and easy. You just add rotation=angle inside plt.xticks(), and you’re done. I often use this when I want a quick fix without modifying individual tick labels.

Method 2: Rotate Tick Labels with ax.set_xticklabels()

If you want more control, especially when using object-oriented Matplotlib, you can rotate tick labels by accessing the Axes object and using set_xticklabels().

Example:

fig, ax = plt.subplots()

ax.bar(cities, sales)
ax.set_xticklabels(cities, rotation=60, ha='right')  # Rotate and align right
ax.set_title('Quarterly Sales by City')
ax.set_xlabel('City')
ax.set_ylabel('Sales (in thousands)')

plt.show()

I executed the above example code and added the screenshot below.

matplotlib xticks rotation

The ability to set horizontal alignment (ha=’right’) helps labels not only rotate but also align neatly, which is great for longer labels like “Los Angeles” or “New York City”

Check out Matplotlib Plot Bar Chart

Method 3: Rotate Tick Labels on the Y-axis

Sometimes, your y-axis labels need rotation, especially when they represent categorical data or dates.

Here’s how I rotated y-axis tick labels while plotting unemployment rates in U.S. states:

states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
unemployment_rate = [7.5, 6.0, 5.8, 6.7, 7.1]

fig, ax = plt.subplots()
ax.barh(states, unemployment_rate)
ax.set_yticklabels(states, rotation=45)
ax.set_title('Unemployment Rate by State')
ax.set_xlabel('Unemployment Rate (%)')
ax.set_ylabel('State')

plt.show()

I executed the above example code and added the screenshot below.

matplotlib rotate x axis labels

When rotating y-axis labels, make sure the rotation angle and alignment don’t make the labels harder to read.

Method 4: Use tick_params() for Rotation

Another approach I use when I want to rotate tick labels without changing the labels themselves is tick_params().

Example:

plt.bar(cities, sales)
plt.tick_params(axis='x', rotation=30)
plt.title('Quarterly Sales by City')
plt.xlabel('City')
plt.ylabel('Sales (in thousands)')
plt.show()

This method is handy because it rotates the ticks while keeping the labels intact, and it works well for quick adjustments.

Read Horizontal Line Matplotlib

Method 5: Rotate Tick Labels with plt.setp()

For fine-tuned control, especially when you want to rotate only specific tick labels, plt.setp() comes in handy.

Here’s how I rotated all x-axis tick labels by 90 degrees:

fig, ax = plt.subplots()
ax.bar(cities, sales)

labels = ax.get_xticklabels()
plt.setp(labels, rotation=90, ha='center')

ax.set_title('Quarterly Sales by City')
ax.set_xlabel('City')
ax.set_ylabel('Sales (in thousands)')

plt.show()

This method is great when you want to customize label properties beyond rotation, like font size or color.

Read Draw a Vertical Line Matplotlib

Tips for Rotating Tick Labels Effectively

  • Choose the right rotation angle: 45 or 90 degrees are common choices. I usually start with 45 degrees and adjust based on label length.
  • Adjust horizontal alignment (ha): When labels are rotated, aligning them to ‘right’ or ‘center’ improves readability.
  • Use tight layout or plt.tight_layout(): Rotated labels can sometimes get clipped. Calling plt.tight_layout() ensures labels are fully visible.
  • Consider font size: Long labels might need smaller fonts to fit well.

Example with tight layout:

plt.bar(cities, sales)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

Rotating tick labels in Matplotlib is a simple yet useful way to enhance your plots’ readability. Whether you prefer the quick plt.xticks() method or the more flexible object-oriented approach, these techniques have helped me create cleaner and more professional visualizations.

If you’re working with datasets involving long categorical labels, especially common in U.S. state or city data, mastering tick label rotation will save you a lot of frustration.

I hope you find these methods useful. Feel free to experiment with different angles and alignments to see what works best for your specific charts.

Other Python articles you may also like:

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.