How can I set axis ticks in Matplotlib? 2

How can I set axis ticks in Matplotlib?

Matplotlib is a popular Python library used for creating visualizations such as charts, graphs, and plots. To effectively display data on a graph, it is important to set appropriate axis ticks. Axis ticks are the small marks or numbers along the x and y axis that indicate the data values being displayed. These ticks help in understanding the scale and range of the data. In Matplotlib, axis ticks can be set using the “xticks” and “yticks” functions, which allow the user to specify the location and labels of the ticks. This ensures that the graph accurately represents the data being presented and makes it easier to interpret and analyze. Overall, setting axis ticks in Matplotlib is a crucial step in creating informative and visually appealing visualizations.

Set Axis Ticks in Matplotlib (With Examples)


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))

The following example shows how to use this syntax in practice.

Example: Set Axis Ticks in Matplotlib

Suppose we use the following code to create a line plot in Matplotlib:

import numpy as np
import matplotlib.pyplotas 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()

By default, Matplotlib has chosen to use a step size of 2.5 on the x-axis and 5 on the y-axis.

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()

Matplotlib set axis ticks

Notice that the step size on the x-axis is now 2 and the step size on the y-axis is 4.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). How can I set axis ticks in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-set-axis-ticks-in-matplotlib/

stats writer. "How can I set axis ticks in Matplotlib?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-set-axis-ticks-in-matplotlib/.

stats writer. "How can I set axis ticks in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-set-axis-ticks-in-matplotlib/.

stats writer (2024) 'How can I set axis ticks in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-set-axis-ticks-in-matplotlib/.

[1] stats writer, "How can I set axis ticks in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I set axis ticks in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top