In this tutorial, we will look at how to change the tick frequency in a matplotlib plot with the help of some examples.

You can change the number of ticks in matplotlib in the following two ways –
- Using the
maplotlib.pyplot
object’sxticks()
and theyticks()
functions to modify the ticks on the x-axis and the y-axis respectively. - You can also use the
maplolib.pyplot
object’slocator_params()
function to specify the number of ticks on a specific axis.
Let’s now look at both methods with some examples.
First, we will create a plot that we will use throughout this tutorial.
import matplotlib.pyplot as plt # x values - years x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020] # y values - curde oil price per barrel in USD y = [94.88, 94.05, 97.98, 93.17, 48.66, 43.29, 50.80, 65.23, 56.99, 39.68] # plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel')
Output:

Example 1 – Change the number of ticks on the x-axis
The matplotlib.pyplot.xticks()
function takes ticks
and labels
as parameters. You can pass the tick locations as an array to the ticks
parameter and the corresponding tick labels to the labels
parameter. In the above plot, we see that there are only 5 tick values, let’s now get a tick for each value in the x
list.
# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # specify the ticks and its frequency plt.xticks(x) # draw the plot plt.draw()
Output:

There are now 10 ticks on the x-axis.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Alternatively, we can use the maplotlib.pyplot.locator_params()
function and only specify the number of ticks we want (instead of specifying the actual tick locations like above).
The matplotlib.pylot.locator_params()
function takes axis
and nbins
as parameters. Specify the axis to which you want to apply these settings with the axis
parameter and the number of ticks you want to the nbins
parameter.
# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # specify the number of ticks on the x-axis plt.locator_params(axis='x', nbins=10) # draw the plot plt.draw()
Output:

We get the same result as above.
Example 2 – Change the number of ticks on the y-axis
You can similarly change the number of ticks on the y-axis.
Use the mapltolib.pyplot.yticks()
function to give custom ticks and tick labels to the plot. Let’s change the number of ticks on the y-axis to 3 in the original scatter plot.
import numpy as np # plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # specify use 3 equally spaced values between min and max values as yticks plt.yticks(np.linspace(min(y), max(y), 3)) # draw the plot plt.draw()
Output:

The y-axis now has only 3 ticks.
Alternatively, use the maplotlib.pyplot.locator_params()
functions and specify the number of ticks for the y-axis.
# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # specify the number of ticks on the y-axis plt.locator_params(axis='y', nbins=3) # draw the plot plt.draw()
Output:

Example 3 – Changing the number of ticks on subplots
You can similarly change the number of ticks (on the x-axis and/or the y-axis) on subplots with the corresponding matplotlib axes object functions.
- Use the axes object’s
set_xticks()
function to change the ticks on the x-axis and theset_yticks()
function to change the ticks on the y-axis. - Alternatively, you can use the axes object’s
locator_params()
function.
Let’s look at an example –
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.plot(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('Crude oil price USD/barrel') # plot the second subplot ax2.plot(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('Crude oil price USD/barrel')
Output:

Here, we created two subplots. Both the subplots have the same data but that is not important. Let’s see how to modify the number of ticks in the above subplots.
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.plot(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('Crude oil price USD/barrel') # change number of ticks to 10 on the x-axis ax1.set_xticks(x) # plot the second subplot ax2.plot(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('Crude oil price USD/barrel') # change number of ticks to 10 on the x-axis ax2.locator_params(axis='x', nbins=10)
Output:

To change the number of ticks on the axis we used –
- For the first subplot, the axes object’s
set_xticks()
function. - For the second subplot, the axes object’s
locator_params()
function.
You might also be interested in –
- Remove Tick Labels from a Plot in Matplotlib
- Set Axis Range (axis limits) in Matplotlib Plots
- Show Gridlines on Matplotlib Plots
- How to Label Points on a Scatter Plot in Matplotlib?
- Reverse Axes of a Plot in Matplotlib
- Rotate Axis Labels in Matplotlib with Examples and Output
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.