The annotate() function in the pyplot module of the matplotlib library is used to get and set the current tick locations and labels of the x-axis.
Matplotlib xticks() is a library function used to define the x-axis points. The xticks() function is used for getting and setting the current tick location and labels of the x-axis.
matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)
The matplotlib.pyplot.xticks() function has two optional arguments as parameter:
- ticks: It is an optional argument. The ticks argument takes an array as a value. This array consists of the x ticks’ location. If an empty list is passed in this argument, then it removes all the xticks in the plot.
- labels: The labels for the x ticks are passed in this argument. The label argument can only be used while passing the value for the x ticks. If both the x ticks and labels are passed as arguments to this function, then the x ticks locations are labeled with the labels passed in the argument.
The xticks() function returns two arrays as return values. One array consists of xticks locations and the other consists of the text labels placed at the axis location.
Program for setting the xticks in a line chart using matplotlib.pyplot.xticks
# Importing matplotlib.pyplot as plt. import matplotlib.pyplot as plt # Importing numpy as np import numpy as np # create a numpy array for storing the x coordinates x = np.arange(0, 100, 10) # create a numpy array for storing the y coordinates y = np.arange(0, 100, 10) # pass the x coordinates and y coordinates into the plot() function plt.plot(x, y, '*-') plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']) # displaying the created graph using the show method plt.show()
In this program, we imported matplotlib.pyplot to plot the line chart and then mark its x-axis locations using the xticks() function. The matplotlib library consists of all the functions for plotting different types of graphs and charts. Then, we imported numpy for creating x coordinates.
The x array consists of values from 0 to 100, with 10 as the interval. Then we created the y array with the same interval. This numpy array consists of x and y coordinates. Then we plotted the line graph using the plot() function.
We passed the format as * followed with – a line. Hence the points are marked with stars and connected with a line. Then we have set the x-axis locations with the labels A to K. Then, we displayed the line chart using the show function. This displays the line graph with the x-axis labeled as A to K.
Program for setting xticks for a bar chart using matplotlib.pyplot.xticks
# Importing matplotlib.pyplot as plt. import matplotlib.pyplot as plt # Importing numpy as np import numpy as np # Bar width is defined barWidth = 0.25 # subplot is created fig = plt.subplots(figsize=(12, 8)) # pass count for science is created science = [20, 30, 15, 8, 20] # pass count for social is created social = [25, 8, 15, 25, 20] # pass count for computer science is created computer_science = [30, 13, 20, 29, 15] br1 = np.arange(len(science)) br2 = [x + barWidth for x in br1] br3 = [x + barWidth for x in br2] # Bar chart is created plt.bar(br1, science, color='black', width=barWidth, edgecolor='grey', label='Science') plt.bar(br2, social, color='yellow', width=barWidth, edgecolor='grey', label='Social') plt.bar(br3, computer_science, color='b', width=barWidth, edgecolor='grey', label='Computer Science') # Xticks is added plt.xlabel('Branch', fontweight='bold', fontsize=15) plt.ylabel('Students pass count', fontweight='bold', fontsize=15) plt.xticks([0.0, 0.25, 0.50, 1, 1.25, 1.50, 2, 2.25, 2.50, 3, 3.25, 3.50, 4, 4.25, 4.50], ['sc', 'so', 'CS', 'sc', 'so', 'CS', 'sc', 'so', 'CS', 'sc', 'so', 'CS', 'sc', 'so', 'CS']) plt.show()
In this program, we imported matplotlib.pyplot for plotting the bar chart and for setting the x-axis location.
The matplotlib library consists of all the functions for plotting different types of graphs. In addition, we have created three arrays for storing the pass count for science, social, and computer science.
Then, we plotted the bar chart for subjects across the year and student pass count. Finally, we plotted the pass count for all three subjects each year. We have labeled the subject name using the xticks function.
We labeled each point on the x-axis with the subject names. We used sc for science, social, and CS for Computer science. We have displayed the bar chart using the show function. It displays the subject names on the x-axis.
That’s it for this tutorial.