Use Matplotlib set_xticklabels to Customize X-Axis Labels in Python

When working with data visualization in Python, Matplotlib is one of my go-to libraries. Over the years, I’ve learned that customizing plots to make them more readable and visually appealing is just as important as the data itself. One key aspect of this customization is the ability to control the x-axis labels effectively. That’s where the set_xticklabels method comes in handy.

In this article, I’ll share my firsthand experience on how to use set_xticklabels to change the appearance and content of x-axis labels in Matplotlib.

Let’s get in!

What is set_xticklabels in Matplotlib?

set_xticklabels is a method in Matplotlib that allows you to set the text labels on the x-axis ticks. This method is useful when you want to replace the default numeric labels with custom strings or modify their appearance for better clarity.

For example, if you are plotting monthly sales data for different US states, you might want to replace the numeric x-axis ticks with the actual month names or abbreviations, and style them to suit your presentation needs.

Read Matplotlib Secondary y-Axis

Basic Usage of set_xticklabels

Let me start with a simple example. Suppose I have sales data for the first quarter:

import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar']
sales = [25000, 27000, 30000]

plt.plot(sales)
plt.xticks([0, 1, 2])  # Set tick positions
plt.gca().set_xticklabels(months)  # Set custom tick labels

plt.title('Quarter 1 Sales')
plt.show()

You can see the output in the screenshot below.

set_xticklabels

Here, I first set the tick positions to [0, 1, 2] because Matplotlib by default uses numeric indices for the x-axis. Then, I replaced those ticks with the month abbreviations using set_xticklabels.

Check out Matplotlib Set Axis Range

Rotate X-Axis Labels for Better Readability

In many cases, especially when labels are long or numerous, they tend to overlap. One of the easiest fixes is to rotate the labels.

Here’s how I do it:

plt.plot(sales)
plt.xticks([0, 1, 2])
plt.gca().set_xticklabels(months, rotation=45)  # Rotate labels by 45 degrees

plt.title('Quarter 1 Sales with Rotated Labels')
plt.show()

You can see the output in the screenshot below.

ax.set_xticklabels

This small tweak makes a big difference in readability, especially when dealing with crowded x-axis labels.

Change Font Size, Color, and Font Weight

Customizing the font properties can help highlight specific data points or match your chart style.

Example:

plt.plot(sales)
plt.xticks([0, 1, 2])
plt.gca().set_xticklabels(months, fontsize=12, color='blue', fontweight='bold')

plt.title('Styled X-Axis Labels')
plt.show()

You can see the output in the screenshot below.

matplotlib set_xticklabels

Changing the font size and color can make your charts look more professional and aligned with your report or dashboard theme.

Read What is the add_axes Matplotlib

Use set_xticklabels with Date Data

When plotting time series data, like daily temperatures across a week in New York City, the x-axis might have dates as ticks. You can customize these labels easily.

import matplotlib.dates as mdates
import pandas as pd

dates = pd.date_range('2025-07-01', periods=7)
temps = [85, 88, 90, 87, 85, 89, 91]

plt.plot(dates, temps)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))  # Format date labels as 'Jul 01', etc.
plt.gca().set_xticklabels(dates.strftime('%b %d'), rotation=30, fontsize=10)

plt.title('NYC Temperatures in Early July 2025')
plt.show()

Here, I used set_xticklabels combined with date formatting to make the x-axis labels meaningful and easy to read.

When to Use set_xticklabels vs. plt.xticks

You might wonder why we sometimes use plt.xticks() alone or combine it with set_xticklabels().

  • plt.xticks() can both set tick positions and labels in one call.
  • set_xticklabels() is used on the axis object and gives more control over styling.

For example:

plt.plot(sales)
plt.xticks([0, 1, 2], months, rotation=60, fontsize=11)
plt.title('Using plt.xticks for Labels')
plt.show()

This is often more concise, but if you need to access the axis object for other customizations, set_xticklabels is useful.

Check out Matplotlib 2d Surface Plot

Avoid Common Issues

One thing I’ve learned is that when using set_xticklabels, you should always set the tick positions explicitly with set_xticks or plt.xticks first. Otherwise, labels might not align correctly with the ticks.

For example, this might cause issues:

plt.plot(sales)
plt.gca().set_xticklabels(['Jan', 'Feb', 'Mar'])  # No tick positions set, labels may misalign
plt.show()

Always pair set_xticklabels with proper tick positioning:

plt.plot(sales)
plt.gca().set_xticks([0, 1, 2])
plt.gca().set_xticklabels(['Jan', 'Feb', 'Mar'])
plt.show()

Read Matplotlib Unknown Projection ‘3d’

Advanced: Use set_xticklabels with Multiple Subplots

When working with multiple subplots, you can customize each subplot’s x-axis labels individually.

fig, axs = plt.subplots(2, 1, figsize=(8, 6))

# First subplot
axs[0].plot(sales)
axs[0].set_xticks([0, 1, 2])
axs[0].set_xticklabels(['Jan', 'Feb', 'Mar'], rotation=45)

# Second subplot with different labels
axs[1].plot([30000, 32000, 31000])
axs[1].set_xticks([0, 1, 2])
axs[1].set_xticklabels(['Apr', 'May', 'Jun'], rotation=0)

plt.tight_layout()
plt.show()

This flexibility is vital when you want each subplot to tell its own story clearly.

Using set_xticklabels effectively can transform your Matplotlib charts from basic to polished. It’s a simple yet powerful tool for anyone serious about data visualization in Python.

If you want to dive deeper into Matplotlib customization, keep experimenting with label properties and explore other axis formatting options.

You may also 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.