Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Set X-Axis Values in Matplotlib in Python?

Improve Article
Save Article
  • Last Updated : 13 Sep, 2022
Improve Article
Save Article

In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language.

The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values.

Syntax:

matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)

xticks() function accepts the following parameters:

Parameters Description
ticks
  • List of xticks locations.
  • Optional
  • Passing an empty list will remove all the xticks.
labels
  • List of labels to place at given ticks location.
  • Optional
**kwargs Text properties used to control the appearance of labels.

Returns: xticks() function returns following values:

  • locs: List of xticks location.
  • labels: List of xlabel text location.

Example #1 :

In this example, we will be setting up the X-Axis Values in Matplotlib using the xtick() function in the python programming language.

Python3




# Python program to set x-axis values in matplotlib
 
import matplotlib.pyplot as plt
 
# x-axis and y-axis values for plotting
x = [1, 2, 3, 4, 5, 6]
y = [3, 1, 4, 5, 3, 6]
 
# labels for x-asix
labels = ['A', 'B', 'C', 'D', 'E', 'F']
 
# Plotting x-axis and y-axis
plt.plot(x, y)
 
# naming of x-axis and y-axis
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
 
# naming the title of the plot
plt.title("Set X-Axis Values in Matplotlib")
 
# setting x-axis values
plt.xticks(x, labels)
 
plt.show()


Output:

Example #2:

In this example, we will use the rotation argument to accept rotation value in degree and it will rotate labels in the clockwise direction by specified degree.

Python3




# Python program to set x-axis values in matplotlib
 
import matplotlib.pyplot as plt
 
# x-axis and y-axis values for plotting
x = [1, 2, 3, 4, 5, 6]
y = [3, 1, 4, 5, 3, 6]
 
# labels for x-asix
labels = ['Label1', 'Label2', 'Label3', 'Label4', 'Label5', 'Label6']
 
# Plotting x-axis and y-axis
plt.plot(x, y)
 
# naming of x-axis and y-axis
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
 
# naming the title of the plot
plt.title("Set X-Axis Values in Matplotlib")
 
# setting x-axis values and specifying rotation
# for the labels in degrees
plt.xticks(x, labels, rotation=45)
 
plt.show()


Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!