I have spent over a decade building data visualizations in Python, and if there is one thing I’ve learned, it’s that default axis ticks are rarely perfect.
Often, Matplotlib tries to be helpful by guessing where your ticks should go, but it often ends up cluttering the x-axis or skipping vital data points.
In this tutorial, I will show you exactly how to control your Python plot axes using set_xticks. We will focus on defining specific ranges and setting “every nth” tick for better readability.
How to Set Python Matplotlib xticks Range
Setting the range of your xticks is the first step in creating a professional-grade Python visualization.
I usually use this when I want to highlight a specific window of time or a specific numerical boundary in a dataset.
Method 1: Use NumPy arange for a Precise xticks Range
One of the most reliable ways I have found to set a range is by using the numpy.arange function.
It allows you to define a start, a stop, and a step value, which you then pass directly into the set_xticks method.
import matplotlib.pyplot as plt
import numpy as np
# Sample Data: Tech Salaries in San Francisco (2010 - 2024)
years = np.array([2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024])
salaries = np.array([95, 102, 110, 115, 125, 138, 145, 155, 168, 175, 180, 195, 210, 215, 225])
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(years, salaries, marker='o', color='#007acc', linewidth=2)
# Setting the xticks range from 2010 to 2024 with a step of 2
# We use 2025 as the stop because the stop value is exclusive in Python
ticks = np.arange(2010, 2025, 2)
ax.set_xticks(ticks)
ax.set_title('Average Tech Salaries in San Francisco (USD In Thousands)', fontsize=14)
ax.set_xlabel('Year', fontsize=12)
ax.set_ylabel('Salary ($1k)', fontsize=12)
ax.grid(True, linestyle='--', alpha=0.6)
plt.show()You can see the output in the screenshot below.

In this Python code, I defined a specific range that starts at 2010 and ends at 2024. By using np.arange(2010, 2025, 2), I told Matplotlib to only show every second year.
Method 2: Use Python List Comprehension for Custom xticks Range
Sometimes, you don’t want a mathematical step. You might want a specific list of years that are relevant to your US-based business report.
I often use list comprehensions when my range is derived from an existing list of objects or filtered data.
import matplotlib.pyplot as plt
# US Home Price Index (Monthly for two years)
months = ["Jan 22", "Feb 22", "Mar 22", "Apr 22", "May 22", "Jun 22",
"Jul 22", "Aug 22", "Sep 22", "Oct 22", "Nov 22", "Dec 22",
"Jan 23", "Feb 23", "Mar 23", "Apr 23", "May 23", "Jun 23"]
index_values = [300, 305, 310, 315, 318, 322, 320, 315, 310, 308, 305, 302, 304, 308, 312, 318, 325, 330]
plt.figure(figsize=(12, 6))
plt.plot(months, index_values, color='green', marker='s')
# Select only the first month of each quarter using Python list slicing
quarterly_ticks = months[::3]
plt.xticks(ticks=range(0, len(months), 3), labels=quarterly_ticks)
plt.title('US Housing Price Index Trend (Quarterly View)', fontsize=14)
plt.ylabel('Index Value')
plt.xticks(rotation=45)
plt.show()You can see the output in the screenshot below.

In this example, I used Python’s slicing syntax [::3] to grab every third month. This is a very “Pythonic” way to handle ranges without importing extra libraries.
How to Set Python Matplotlib xticks Every Nth Value
When dealing with high-frequency data, like daily stock prices for the S&P 500, showing every single date on the x-axis is impossible.
You need to tell Python to display a tick “every” N units to maintain clarity.
Method 1: Use the MultipleLocator for “Every N” Ticks
The matplotlib.ticker module is a powerful tool that I frequently use for professional financial charts.
The MultipleLocator is specifically designed to place ticks at regular intervals (every Nth value) automatically.
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
# Simulation: Daily Amazon (AMZN) Stock Price over 100 days
days = np.arange(1, 101)
prices = 150 + np.cumsum(np.random.standard_normal(100) * 2)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(days, prices, color='orange')
# Set xticks to appear every 10 days
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.set_title('Simulated AMZN Stock Price (100 Trading Days)', fontsize=14)
ax.set_xlabel('Day Count')
ax.set_ylabel('Price (USD)')
ax.grid(axis='x', alpha=0.3)
plt.show()You can see the output in the screenshot below.

I prefer MultipleLocator because it is dynamic. If you zoom into the plot, the “every 10” logic remains consistent, which is much better than hard-coding a list.
Method 2: Use Slicing with set_xticks for Every Nth Label
If you are working with categorical data or fixed strings (like names of US Cities), the locator might not work as expected.
In these cases, I manually slice the data to show “every” Nth label.
import matplotlib.pyplot as plt
# US Cities by Population Density (Simplified Sample)
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix",
"Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"]
density = [27012, 8485, 11847, 3613, 3120, 11797, 3238, 4325, 3866, 5777]
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(cities, density, color='skyblue')
# Show every 2nd city to avoid overlap
n = 2
ax.set_xticks(range(0, len(cities), n))
ax.set_xticklabels(cities[::n])
ax.set_title('Population Density of Major US Cities', fontsize=14)
ax.set_ylabel('People per Square Mile')
plt.show()You can see the output in the screenshot below.

This method is excellent for bar charts. It ensures that the tick positions and the labels stay synchronized by using the same step n.
Pro Tips for Managing xticks in Python
Throughout my career, I have found that simply setting the range is only half the battle. To make your Python plots truly stand out, consider these adjustments:
- Rotation: If your labels are long (like “San Francisco”), use plt.xticks(rotation=45). This prevents text from crashing into each other.
- Alignment: When you rotate labels, set the ha=’right’ (horizontal alignment) so the end of the text aligns with the tick mark.
- Font Size: For complex US economic dashboards, reducing the labelsize helps fit more information without clutter.
I hope this tutorial has been helpful to you and that you now feel confident using set_xticks in Python.
By mastering the range and interval of your axes, you can transform a cluttered mess into a clear, professional story.
The key is to always think about your audience. If they can’t read the labels, the data doesn’t matter.
You may read:
- Use tight_layout Colorbar and GridSpec in Matplotlib
- Use Matplotlib fill_between where and alpha for Data Visualizations
- Control Horizontal and Vertical Alignment of xticklabels in Matplotlib
- Customize xtick Labels Using fontdict and fontsize in Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.