Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For fixed custom x-axis labels in Matplotlib, pair each label with its position using ax.set_xticks(positions, labels). The older ax.set_xticklabels() method is discouraged in the current Matplotlib 3.11.2 documentation because labels can become detached from tick positions.

Set custom labels and positions together

Use Axes.set_xticks(positions, labels) when a plot needs specific category names at known x coordinates. Each label stays paired with its intended position:

import matplotlib.pyplot as plt

values = [12, 18, 9]
positions = [0, 1, 2]
labels = ["North", "Central", "South"]

fig, ax = plt.subplots()
ax.bar(positions, values)
ax.set_xticks(positions, labels)
ax.set_xlabel("Region")
fig.tight_layout()
plt.show()

This is the recommended approach in the Matplotlib Axes API. It is suited to a final plot with deliberate, fixed categories. Fixed ticks do not necessarily adapt when users pan or zoom; for interactive plots, use a locator and formatter that can respond to changing ticks.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use set_xticklabels only when you have fixed the ticks

Axes.set_xticklabels() is discouraged because it depends on tick positions. If you are updating existing code that uses it, set the positions first, then provide exactly one label for each position:

positions = [0, 1, 2]
labels = ["North", "Central", "South"]

ax.set_xticks(positions)
ax.set_xticklabels(labels)

The labels are applied in tick order, not matched to numeric x values. Under the hood, set_xticklabels uses a FixedFormatter, which should be paired with a FixedLocator so the tick locations stay fixed. The Axis.set_ticklabels API documents the method’s limitations and the requirement that the label count match the tick locations.

Use a formatter when labels come from tick values

If labels should be calculated from whatever tick values Matplotlib chooses, use a formatter rather than a hard-coded list. For example, FuncFormatter can format numeric ticks as currency:

from matplotlib.ticker import FuncFormatter

ax.xaxis.set_major_formatter(
    FuncFormatter(lambda x, pos: f"${x:,.0f}")
)

The formatter receives the tick value and its position and returns the displayed text. This keeps the label rule tied to the value as the locator selects ticks. The ticker API also documents StrMethodFormatter and specialized date and scale-aware locator/formatter options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fix common label problems

  • Labels appear shifted or change after plotting: Set positions and labels together with set_xticks(positions, labels), or fix the tick positions before calling set_xticklabels.
  • The number of labels does not match the number of ticks: Make both sequences the same length, with one label for each tick location.
  • Labels should represent values rather than category positions: Use a value-aware formatter such as FuncFormatter instead of assigning a fixed list.
  • Ticks should respond to pan or zoom: Use an automatic locator with a formatter so ticks and labels can adjust to the view.
  • You only want to change tick appearance: Prefer set_tick_params for tick styling. Label keyword arguments to set_xticklabels affect current tick objects and may not persist if ticks are regenerated.

See Matplotlib’s Axis ticks guide for more on choosing between fixed ticks and automatic tick behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.