Table of Contents
Data visualization is a critical component of modern data analysis, and Matplotlib stands as the foundational plotting library in the Python ecosystem. While Matplotlib excels at automatically generating informative plots, controlling the appearance and density of axis ticks is often necessary to enhance readability and convey specific insights. Default settings sometimes result in tick marks that are too sparse or too crowded, obscuring the underlying data patterns. This comprehensive guide will demonstrate precisely how to manually set and customize the steps and positions of axis ticks using the powerful combination of Matplotlib’s dedicated functions and NumPy arrays.
The ability to precisely define where tick marks appear along both the horizontal (x) and vertical (y) axes is essential for creating publication-quality graphics. For instance, if your data represents quarterly reports or measurements taken every two units, setting standard interval steps ensures that the visual representation aligns perfectly with the underlying data structure. We primarily rely on two core Matplotlib functions—plt.xticks() and plt.yticks()—which accept a list or array of numerical positions where the ticks should be placed.
Setting Axis Ticks Using Standard Matplotlib Functions
To modify the placement of ticks, we leverage the xticks and yticks functions available through the matplotlib.pyplot interface, typically imported as plt. These functions require an iterable object—usually a NumPy array—that specifies the exact coordinates for the desired tick marks. The most efficient way to generate these evenly spaced coordinates is by utilizing the arange function provided by the NumPy library. This synergy between plotting and numerical computation libraries is a hallmark of scientific Python programming.
The fundamental syntax involves calculating the minimum and maximum range of your data, and then defining a consistent step size. This approach provides granular control over the visual presentation, ensuring that every significant data point is properly represented by a corresponding tick label.
You can use the following basic syntax to set the axis ticks in a Matplotlib plot:
#set x-axis ticks (step size=2) plt.xticks(np.arange(min(x), max(x)+1, 2)) #set y-axis ticks (step size=5) plt.yticks(np.arange(min(y), max(y)+1, 5))
This code snippet clearly illustrates the mechanism. We call plt.xticks (or plt.yticks) and pass it the output of np.arange. The np.arange function is critical here; it generates a sequence of numbers starting at min(data), ending just before the specified stop value (hence max(data) + 1 to ensure the maximum value is included), and advancing by the specified step size (2 for x, 5 for y). This method ensures that the generated ticks span the entire range of the plotted data effectively.
The Role of numpy.arange in Defining Tick Positions
When working with numerical data in Python, especially for visualization purposes, the numpy.arange function becomes indispensable. Unlike Python’s built-in range() function, np.arange returns a NumPy array, which is the preferred input format for Matplotlib’s tick functions, making the process highly efficient. Understanding its parameters—start, stop, and step—is the key to mastering axis customization.
The syntax for np.arange(start, stop, step) is straightforward but requires careful consideration of the ‘stop’ parameter. The resulting array includes the ‘start’ value but strictly excludes the ‘stop’ value. Therefore, when setting ticks, we often calculate the range based on min(data) and ensure the upper bound is slightly greater than max(data) to guarantee that the final data point falls within the defined axis boundaries and receives a tick mark if desired. Adding +1, or a small epsilon, to the maximum value of the dataset is a common programming practice to guarantee inclusion.
For instance, if your x-data ranges from 0 to 20, and you want ticks every 2 units, you would use np.arange(0, 21, 2). This generates the sequence [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]. If you had simply used np.arange(0, 20, 2), the value 20 would have been omitted, potentially leading to a confusing visualization where the edge of the data is unlabeled. Precision in setting these boundaries is paramount for accurate data representation.
Example: Observing Default Tick Behavior in Matplotlib
Before customizing the axes, it is useful to see how Matplotlib handles tick placement automatically. By default, Matplotlib employs sophisticated algorithms, known as locators, that attempt to choose aesthetically pleasing and informative intervals. However, these automated choices may not always align with the semantic meaning or the desired granularity of the data being plotted, necessitating manual intervention. This initial example demonstrates a standard line plot creation without any explicit tick modification.
The following example shows how to use this syntax in practice.
Suppose we use the following code to create a line plot in Matplotlib:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = [0, 2, 7, 10, 12, 15, 18, 20]
y = [0, 5, 9, 13, 19, 22, 29, 36]
#create line plot
plt.plot(x,y)
#display line plot
plt.show()
The data defined above represents simple numerical sequences for both the x and y coordinates. The plt.plot(x, y) command generates the visual representation, and plt.show() renders the figure. Observing the output is crucial for identifying where the default settings fall short of our visualization goals, particularly concerning the interval spacing on the axes.

Upon reviewing the resulting graph, we can clearly see Matplotlib’s automatic tick allocation. By default, Matplotlib has chosen to use a step size of 2.5 on the x-axis (ticks appear at 0, 2.5, 5.0, 7.5, 10.0, etc.) and 5 on the y-axis (ticks appear at 0, 5, 10, 15, etc.). While functional, the 2.5 step size on the x-axis might introduce unnecessary decimal points, potentially cluttering the visualization if only integer or even-numbered intervals are relevant to the interpretation of the dataset. This scenario perfectly illustrates the need for manual adjustment to achieve optimal clarity.
Implementing Custom Axis Tick Intervals
To address the issues presented by the default tick placement, we utilize plt.xticks and plt.yticks in conjunction with np.arange to enforce specific, meaningful intervals. In this example, we will enforce an even step size of 2 on the x-axis and a step size of 4 on the y-axis. These adjustments ensure that the grid lines and labels correspond to intervals that are easier for the viewer to interpret, especially when dealing with data points that naturally fall on these specific boundaries.
We can use the following code to change the step size on each axis:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = [0, 2, 7, 10, 12, 15, 18, 20]
y = [0, 5, 9, 13, 19, 22, 29, 36]
#create line plot
plt.plot(x,y)
#specify axis tick step sizes
plt.xticks(np.arange(min(x), max(x)+1, 2))
plt.yticks(np.arange(min(y), max(y)+1, 4))
#display line plot
plt.show()
In this revised script, the lines defining the tick placement are strategically placed just before the plt.show() command. For the x-axis, np.arange(0, 21, 2) generates ticks at 0, 2, 4, …, 20. For the y-axis, where the data maximum is 36, np.arange(0, 37, 4) ensures ticks appear at 0, 4, 8, 12, …, 36. This explicit declaration overrides Matplotlib’s default locators, giving the programmer full control over the axis grid.
The result is a plot that is significantly cleaner and more aligned with integer boundaries, particularly on the x-axis where the decimal step size has been eliminated. This meticulous control over visual elements is what differentiates standard plots from professional, high-impact visualizations suitable for reports or academic publications.

Notice that the step size on the x-axis is now 2 (ticks at 0, 2, 4, 6, etc.) and the step size on the y-axis is 4 (ticks at 0, 4, 8, 12, etc.). This demonstrates the successful implementation of custom intervals.
Advanced Tick Customization: Major and Minor Ticks
While setting major ticks using plt.xticks and plt.yticks is sufficient for most common plotting scenarios, Matplotlib offers even finer control through the concept of Major Ticks and Minor Ticks. Major ticks are typically the ones labeled and used for primary grid lines, while minor ticks provide additional visual guidance for interpolation between major ticks but are usually smaller and unlabeled. Utilizing both types can significantly improve the density of information without overwhelming the viewer with too many labels.
To manage major and minor ticks, we often interact directly with the Matplotlib ticker module and the specific axes object (e.g., ax.xaxis or ax.yaxis), rather than the global plt.xticks functions. This requires obtaining the current axes object, which is best practice for highly customized plots. Locators are the objects responsible for determining where ticks should be placed.
Common locators include the MultipleLocator, which places ticks at integer multiples of a specified base, and the AutoMinorLocator, which automatically determines appropriate minor tick locations based on the major tick interval. For example, if major ticks are set every 10 units, minor ticks might be automatically placed every 2 units. This approach separates the primary labeling task from the secondary visual aid task.
Major Ticks: These define the primary positions and are usually labeled. They are set using
plt.xticksor by assigning a MajorLocator object to the axis.Minor Ticks: These are smaller, unlabeled marks that subdivide the space between major ticks, improving reading precision.
When defining both sets of ticks, the process becomes slightly more involved, requiring interaction with the figure and axes objects directly. For instance, to set major ticks every 10 units and minor ticks every 2 units on the y-axis, one would use:
from matplotlib import ticker
fig, ax = plt.subplots()
ax.plot(x, y)
# Set major ticks every 10 units
major_locator = ticker.MultipleLocator(10)
ax.yaxis.set_major_locator(major_locator)
# Set minor ticks every 2 units
minor_locator = ticker.MultipleLocator(2)
ax.yaxis.set_minor_locator(minor_locator)
plt.show()
While np.arange is perfect for quick, array-based tick setting using plt.xticks, utilizing the ticker module with locators offers a more robust and object-oriented way to handle complex axis formatting and ensures clean separation between major and minor tick definitions.
Summary of Best Practices for Axis Control
Effective control over axis ticks significantly impacts the narrative conveyed by a visualization. While Matplotlib’s defaults are excellent starting points, tailoring the axes to the specific data context is a hallmark of high-quality data presentation. Here is a summary of best practices when customizing axis ticks in Matplotlib:
Prioritize Readability: Always choose tick intervals that are logically consistent with the data being plotted (e.g., using steps of 1, 2, 5, 10, or 100, rather than arbitrary decimals like 2.3).
Use NumPy for Generation: For defining evenly spaced numerical ticks, rely on the efficiency and array output of
numpy.arange, ensuring the stop value correctly includes the maximum data point.Ensure Full Coverage: Always check that your defined tick range spans the entire extent of your data, preventing the plot edges from being unlabeled or misleading.
Consider Locators for Complexity: When dealing with minor ticks, logarithmic scales, or complex date formatting, transition from simple
plt.xtickscalls to using the dedicatedmatplotlib.tickerlocators (likeMultipleLocatororLogLocator) for enhanced flexibility.Formatting Labels: Remember that
plt.xticksandplt.yticksalso accept an optional second argument for label text. This allows you to place ticks at numerical positions (e.g., 1, 2, 3) but display custom labels (e.g., ‘Q1’, ‘Q2’, ‘Q3’).
Mastering these techniques provides the foundation for creating insightful and error-free graphical representations of complex datasets. The slight effort required to manually define tick placement often yields significant returns in terms of plot clarity and professional appearance.
Additional Resources
The ability to customize tick marks is just one facet of advanced Matplotlib usage. Exploring other aspects of axis control, formatting, and error handling will further solidify your skills in Python data visualization. The following resources provide further guidance on refining your plotting techniques and addressing other common data visualization challenges.
Cite this article
Mohammed looti (2025). Learn How to Customize Axis Ticks in Matplotlib with Examples. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/set-axis-ticks-in-matplotlib-with-examples/
Mohammed looti. "Learn How to Customize Axis Ticks in Matplotlib with Examples." PSYCHOLOGICAL STATISTICS, 1 Nov. 2025, https://statistics.arabpsychology.com/set-axis-ticks-in-matplotlib-with-examples/.
Mohammed looti. "Learn How to Customize Axis Ticks in Matplotlib with Examples." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/set-axis-ticks-in-matplotlib-with-examples/.
Mohammed looti (2025) 'Learn How to Customize Axis Ticks in Matplotlib with Examples', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/set-axis-ticks-in-matplotlib-with-examples/.
[1] Mohammed looti, "Learn How to Customize Axis Ticks in Matplotlib with Examples," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.
Mohammed looti. Learn How to Customize Axis Ticks in Matplotlib with Examples. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.