How to Set X-Axis Values in Matplotlib in Python?
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 |
|
labels |
|
**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:
Please Login to comment...