How can I set the X-axis values in Matplotlib? 2

How can I set the X-axis values in Matplotlib?

Matplotlib is a popular Python library used for data visualization. It allows users to create various types of graphs and charts, including line, bar, and scatter plots. One of the essential features of Matplotlib is the ability to customize the axes of a graph, including the X-axis. This can be achieved by using the “plt.xticks()” function, which allows users to set the values and labels for the X-axis. By providing a list of values, users can control the spacing and placement of the tick marks on the X-axis. This feature is beneficial for creating more precise and visually appealing graphs, especially when working with large datasets.

Set X-Axis Values in Matplotlib


You can use the following syntax to set the x-axis values for a plot in Matplotlib:

#specify x-axis locations
x_ticks = [2, 4, 6, 8, 10]

#specify x-axis labels
x_labels = ['A', 'B', 'C', 'D', 'E'] 

#add x-axis values to plot
plt.xticks(ticks=x_ticks, labels=x_labels)

The following examples show how to use this syntax in practice.

Example 1: Set X-Axis Values at Equal Intervals

The following code shows how to set the x-axis values at equal intervals in Matplotlib:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)

#specify x-axis locations
x_ticks = [2, 4, 6, 8, 10]

#specify x-axis labels
x_labels = ['A', 'B', 'C', 'D', 'E'] 

#add x-axis values to plot
plt.xticks(ticks=x_ticks, labels=x_labels)

Notice that each x-axis value appears at equally spaced intervals.

Example 2: Set X-Axis Values at Unequal Intervals

The following code shows how to set the x-axis values at unequal intervals in Matplotlib:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)

#specify x-axis locations
x_ticks = [1, 2, 6, 10]

#specify x-axis labels
x_labels = [1, 2, 6, 10] 

#add x-axis values to plot
plt.xticks(ticks=x_ticks, labels=x_labels)

Example 3: Set X-Axis Values at Data Points Only

The following code shows how to set the x-axis values at the data points only:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)#specify x-axis labels
x_labels = ['A', 'B', 'C'] 

#add x-axis values to plot
plt.xticks(ticks=x, labels=x_labels)

Note: You can find the complete documentation for the plt.xticks() function .

Cite this article

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

stats writer. "How can I set the X-axis values in Matplotlib?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-set-the-x-axis-values-in-matplotlib/.

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

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

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

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

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