Make Matplotlib X-Axis Labels Vertical in Python

When I first started working with Python’s Matplotlib library over seven years ago, one issue that frequently came up was how to display x-axis labels vertically.

This problem is common, especially when you have long category names or many data points on your chart. Horizontal labels often overlap, making the graph hard to read.

Over time, I discovered several effective ways to rotate x-axis labels vertically, improving the clarity and professionalism of my visualizations. In this article, I will share my firsthand experience and walk you through multiple methods to make Matplotlib x-axis labels vertical in Python.

Rotate X-Axis Labels Vertically in Python

When plotting data with Python’s Matplotlib, the x-axis often represents categories such as U.S. states, months, or product names. Sometimes these labels are lengthy or numerous, causing them to overlap when displayed horizontally. This overlap makes it difficult for viewers to understand the graph quickly.

Rotating the labels vertically (or at an angle) solves this problem by giving each label enough space and preventing clutter. This simple adjustment improves readability and makes your charts look more polished.

Method 1: Use plt.xticks() to Rotate X-Axis Labels Vertically

The quickest way to rotate x-axis labels vertically is by using the plt.xticks() function with the rotation parameter.

Here’s an example where I plot the average monthly temperatures of five major U.S. cities and rotate the month labels vertically:

import matplotlib.pyplot as plt

# Sample data: Average monthly temperatures (Fahrenheit) for 5 U.S. cities
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
ny_temps = [39, 42, 50, 61, 71, 79, 84, 83, 76, 64, 54, 44]

plt.figure(figsize=(10, 6))
plt.plot(months, ny_temps, marker='o', color='blue', label='New York')

# Rotate x-axis labels vertically
plt.xticks(rotation=90)

plt.title('Average Monthly Temperatures in New York (°F)')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.legend()
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Make Matplotlib X-Axis Labels Vertical

In this example, the rotation=90 argument rotates the labels 90 degrees, making them vertical. This is an easy and effective way to avoid label overlap.

Method 2: Use ax.set_xticklabels() for More Control

If you prefer working with Matplotlib’s object-oriented interface (which I highly recommend for larger projects), you can rotate labels using set_xticklabels() on the Axes object.

Here’s how I used it in a project analyzing U.S. state sales data:

import matplotlib.pyplot as plt

states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
sales = [250, 200, 150, 180, 130]

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

# Rotate x-axis labels vertically
ax.set_xticklabels(states, rotation=90)

ax.set_title('Quarterly Sales by State')
ax.set_ylabel('Sales (in millions)')
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Make Matplotlib X-Axis Labels Vertical in Python

Note: When using set_xticklabels(), sometimes you need to call plt.tight_layout() to prevent label cutoff.

Method 3: Rotate Labels with tick_params()

Another approach I often use, especially when I want to customize tick label appearance further, is tick_params().

Example:

import matplotlib.pyplot as plt

products = ['Smartphones', 'Laptops', 'Tablets', 'Desktops', 'Accessories']
revenue = [120, 95, 60, 40, 30]

fig, ax = plt.subplots(figsize=(7, 5))
ax.bar(products, revenue, color='orange')

# Rotate x-axis labels vertically using tick_params
ax.tick_params(axis='x', rotation=90)

ax.set_title('Product Revenue in Q3')
ax.set_ylabel('Revenue (in millions)')
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Make Matplotlib X-Axis Labels Vertical in Python

This method is handy when you want to rotate labels and simultaneously adjust other tick parameters like label size or color.

Method 4: Use plt.setp() for Batch Label Formatting

For advanced control, I sometimes use plt.setp() to set properties for multiple labels at once.

Example:

import matplotlib.pyplot as plt

regions = ['Northeast', 'Midwest', 'South', 'West']
population = [55, 68, 125, 78]

bars = plt.bar(regions, population, color='purple')

# Get current x-axis tick labels
labels = plt.gca().get_xticklabels()

# Rotate labels vertically
plt.setp(labels, rotation=90, fontsize=12, color='red')

plt.title('Population by U.S. Region (Millions)')
plt.ylabel('Population')
plt.tight_layout()
plt.show()

This approach allows you to customize font size, color, and rotation in one call.

Tips for Using Vertical X-Axis Labels in Python Matplotlib

  • Adjust Figure Size: When labels are vertical, increase the plot width or height figsize to avoid crowding.
  • Use tight_layout() or constrained_layout=True: These help prevent labels from being cut off.
  • Combine with Horizontal Alignment: You can set ha=’right’ or ha=’center’ in rotation methods to align labels better.
  • Consider Angled Labels: Sometimes a 45-degree rotation offers a good balance between readability and space.
  • Test with Real Data: Always check your plots with actual data to ensure labels don’t overlap.

Mastering how to rotate Matplotlib x-axis labels vertically in Python has significantly improved my data visualization skills. It’s a simple tweak that enhances clarity, especially when dealing with U.S.-specific datasets like state sales, city temperatures, or product categories.

With these methods, you can easily customize your charts to fit your audience’s needs and present data professionally. Whether you use plt.xticks(), ax.set_xticklabels(), or other techniques, rotating labels is an essential skill for any Python data analyst or developer.

You may read:

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.