HOWTO · Matplotlib

How to Set X Axis Values in Matplotlib

This tutorial shows how to set X axis values using matplotlib.pyplot.xticks() in Matplotlib. Learn to customize tick frequency, rotate labels, and improve the clarity of your data visualizations. Discover techniques to enhance your plots and make them more informative.

On this page

Use matplotlib.pyplot.xticks() to choose where X-axis tick marks appear and, optionally, what text appears at those positions. Tick positions describe the horizontal coordinates; they do not change the plotted data or set the axis title. For a readable plot, make each label correspond to the position and data point it describes.

Set X-Axis Tick Positions and Labels

The first argument to xticks() is a sequence of positions. The optional second argument supplies one label for each position. Keep those sequences the same length and in the same order. The following example shows custom labels, numeric spacing, an evenly spaced index alternative, and a reduced set of rotated category labels in one figure.

import matplotlib.pyplot as plt

x = [1, 2, 5, 9]
y = [3, 5, 4, 8]
labels = ['Jan', 'Feb', 'May', 'Sep']
categories = ['North region', 'South region', 'East region', 'West region', 'Central region']
values = [12, 9, 14, 7, 11]

fig, axes = plt.subplots(2, 2, figsize=(10, 7))

axes[0, 0].plot(x, y, marker='o')
axes[0, 0].set_title('Custom labels')
axes[0, 0].set_xlabel('Month')
axes[0, 0].set_ylabel('Value')
plt.sca(axes[0, 0])
plt.xticks(x, labels)

axes[0, 1].plot(x, y, marker='o')
axes[0, 1].set_title('Numeric spacing')
axes[0, 1].set_xlabel('X value')
axes[0, 1].set_ylabel('Value')

index_positions = list(range(len(x)))
axes[1, 0].plot(index_positions, y, marker='o')
axes[1, 0].set_title('Evenly spaced positions')
axes[1, 0].set_xlabel('Month')
axes[1, 0].set_ylabel('Value')
axes[1, 0].set_xticks(index_positions)
axes[1, 0].set_xticklabels(labels)

axes[1, 1].bar(categories, values)
axes[1, 1].set_title('Fewer, rotated category labels')
axes[1, 1].set_ylabel('Value')
axes[1, 1].set_xticks([0, 2, 4])
axes[1, 1].set_xticklabels(categories[::2])
plt.sca(axes[1, 1])
plt.xticks(rotation=35, ha='right')

fig.tight_layout()
plt.show()

The top-left plot pairs four positions with four month labels. The numeric plot uses the actual x values, so the distance from 2 to 5 is wider than the distance from 1 to 2. The lower-left plot instead uses positions 0 through 3; this gives the months equal visual spacing while keeping each value paired with its month. The final plot places five bars and labels only positions 0, 2, and 4 to reduce crowding, then rotates and right-aligns those labels. Run the script in a Python environment with Matplotlib installed; no captured output is included here.

Handle Unevenly Spaced X Values

A numeric axis represents numeric distance. If your measurements occur at x values 1, 2, 5, and 9, plotting against those values correctly leaves larger gaps between 2 and 5 and between 5 and 9. Setting the tick labels to months does not change those coordinates. Keep that spacing when distance or elapsed time matters.

If the goal is to compare categories at equal intervals, plot the y values against index positions such as range(len(x)), then use the category names as tick labels. The data sequence and labels must follow the same order. This changes the horizontal scale from numeric distance to category order, so it is unsuitable when the gaps between numeric measurements carry meaning.

For labels that describe dates, use date values as the plotted x data and format the date ticks as needed; assigning month names to unrelated numeric coordinates does not make them dates.

Control Tick Frequency and Improve Label Readability

For a small plot, pass explicit positions to xticks() to show only selected ticks. For example, plt.xticks(x[::2]) selects every other value from an ordered sequence. When using a regular numeric scale, a locator can choose a useful interval as the view changes; for example, import MultipleLocator from matplotlib.ticker and call ax.xaxis.set_major_locator(MultipleLocator(2)) to request major ticks at intervals of two units. Choose explicit positions when the labels are meaningful categories, and a locator when the axis is numeric.

Long labels can be rotated with plt.xticks(rotation=35, ha='right'); ha sets their horizontal alignment. Increase the figure width or use a smaller number of ticks if rotated labels still collide. tight_layout() can make room around axes, but it does not choose which ticks are appropriate.

The example above uses set_xticks on individual axes for most panels and xticks on the current axes for label customization. With the object-oriented style, ax.set_xticks(positions, labels) keeps the operation tied to a specific axes. With pyplot, select the intended axes first when a figure contains multiple plots.

FAQ: Setting X-Axis Values

Can I set positions and labels in one call? Yes. Supply both sequences to plt.xticks(positions, labels) or use ax.set_xticks(positions, labels). Each label maps by index to its corresponding position.

How do I show fewer ticks? Provide a shorter list of tick positions, or use a locator for regularly spaced numeric ticks. Tick frequency is separate from the number of plotted data points.

Can I use text categories on the X-axis? Yes. Plot category values directly, as in the bar chart, or plot numeric index positions and assign the category names as labels. Use the latter when you need a specific category order or equal spacing.

What should I try when labels overlap? Show fewer labels, rotate them, right-align rotated text, or make the figure wider. These changes affect tick labels; they do not alter the plotted values.