In this Python Matplotlib tutorial, we will discuss Matplotlib set_xticklabels in matplotlib. Here we will cover different examples related to set_xticklabels using matplotlib. And we will also cover the following topics:
- Matplotlib set_xticklabels
- Matplorlib set_xticklabels fontdict
- Matplotlib set_xticklabels fontsize
- Matplotlib set_xticklabels font
- Matplotib set_xticklabels color
- Matplotlib set_xticklabels vertical
- Matplotlib set_xticklabels horizontal alignment
- Matplotlib set_xticklabels rotation
- Matplotlib set_xticklabels hide
- Matplotlib set_xticklabels alignment
- Matplotlib set_xticklabels minor
- Matplotlib boxplot set_xticklabels
Matplotlib set_xticklabels
In this section, we learn about the set_xticklabels() function in the axes module of matplotlib in Python. The set_xticklabels function is used to set the x-tick labels with the list of string labels.
The syntax is given below:
matplotlib.axes.Axes.set_xticklabels(labels, fontdict=None, minor=False, **kwargs)
The following are the parameters used above:
Parameters | Value | Default | Description |
labels | list of string | Specifies the list of string labels | |
fontdict | dict | { ‘fontsize’ : rcParams[ ‘ axes.titlesize ‘ ], ‘fontweight’ : rcParams[ ‘ axes.titleweight ‘ ], ‘verticalalignment’ : ‘baseline’, ‘horizontalalignment’ : loc } | This parameter is used to control the appearance of the ticklabels. |
minor | bool | False | This parameter is used to specify whether to set minor ticklabels rather than the major ticklabels. |
Warning
This method only be used after fixing the tick positions using Axes.set_xticks.
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 100)
y1 = np.sin(x)
# Plot
ax.plot(x, y1)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'])
# Add fig title
fig.suptitle('set_xticklabels()function Example', fontweight ="bold")
# Display
plt.show()
- In the above example, firstly we import numpy and matplotlib library.
- After this, we create a subplot by using the subplots() function.
- To define data coordinates, we use linespace() and sin() methods.
- To plot the graph between x and y data coordinates, we use plot() function.
- To fix the position of ticks, use set_xticks() function.
- To set string labels at x-axis tick labels, use set_xticklabels() function.
- To add suptitle, we use the suptitle() function of the figure class.
- To visualize the plot on the user’s screen, use the show() function.
Matplotlib set_xticklabels fontdict
We’ll learn how to use the fontdict parameter of the set_xticklabels method. fondict parameter is a dictionary used to control the appearance of the ticklabels.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels, fontdict=None)
Example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 100)
y1 = np.cos(x)
# Plot
ax.plot(x, y1)
# fontdict dictionary
font_dict = {'fontsize': 20, 'fontweight': 'heavy', 'verticalalignment':'top'}
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], fontdict=font_dict)
# Add fig title
fig.suptitle('set_xticklabels fontdict Example', fontweight ="bold")
# Display
plt.show()
Here we pass the fontdict parameter to the set_xticklabels function. We create a dictionary font_dict to change the appearance of ticklabels with the following key and value:
key | value |
fontsize | 20 |
fontweight | heavy |
verticalalignment | top |
Output:
Matplotlib set_xticklabels fontsize
Here we’ll learn how we can modify the font size of x-axis tick labels. To change the size, we have to pass the fontsize argument to the set_xticklabels method.
The following is the syntax:
matplotlib.axes.Axes.set_xtciklabels(labels, fontsize=None)
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 100)
y = np.cos(30*x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], fontsize=5.5)
# Add fig title
fig.suptitle('set_xticklabels fontsize Example', fontweight ="bold")
# Display
plt.show()
- In the above example, we set text labels at x-axis by using set_xticklabels function and we pass fontsize argument to the function to change font size of the ticklabels.
- We assign 5.5 pt value to fontsize argument.
Read Stacked Bar Chart Matplotlib
Matplotlib set_xtciklabels font
We’ll learn how to change the font style of tick labels at the x-axis. To change the style we pass the fontstyle parameter to the set_xticklabels method.
The following is the syntax:
matplotlib.axes.Axesset_xticklabels(labels, fontstyle=None)
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 6 * np.pi, 250)
y = np.sin(60*x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, 2 * np.pi, 4 * np.pi, 6 * np.pi])
ax.set_xticklabels(['0', r'2$\pi$', r'4$\pi$', r'6$\pi$'], fontstyle='italic')
# Add fig title
fig.suptitle('set_xticklabels fontstyle Example', fontweight ="bold")
# Display
plt.show()
Here we change the font style of tick labels at the x-axis and set it to italic.
Matplotlib set_xticklabels color
Here we change the font style of tick labels at x-axis and set it to italic. Here we’ll learn to change the color of xticklabels. For this we have to pass the color argument to set_xticklabels method.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels, color=None)
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 100)
y = np.sin(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], color='red', fontsize=15)
# Add fig title
fig.suptitle('set_xticklabels color Example', fontweight='bold')
# Display
plt.show()
- In the above example, we define data coordinates using linespace() and sin() methods and to plot them we use, plot() method.
- After this, we use set_xticks() method for fixing the position of ticks at x-axis.
- Then we use set_xticklabels() method for setting string labels at axis. And to change the color of ticklabels we pass color argument to method.
By default, color of ticklabels is black, now we change it red.
Matplotlib set_xticklabels vertical
Here we’ll learn to change vertical alignment of xticklabels . To change the alignment pass verticalalignment argument to set_xticklabels() method.
In place of verticalalignment you can also write va.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(lables, verticalalignemnt='center' | 'top' | 'bottom' | 'baseline')
4 different vertical alignments are:
- center
- top
- bottom
- baseline
verticalalignment=’center’
Example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 100)
y = np.tan(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], verticalalignment='center')
# Add fig title
fig.suptitle('set_xticklabels verticalalignment Example', fontweight='bold')
# Display
plt.show()
- Here we use the linespace() and tan() methods to define data coordinates.
- After this, we use the plot() method to plot a graph between x and y coordinates.
- To set the tick marks, use set_xticks() method.
- To set the tick labels in string format, we use the set_xticklabels() method.
- Here we set the verticalalignemnt of tick labels to the center.
Verticalalignemnt=’top’
Example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 100)
y = np.tan(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], verticalalignment='top')
# Add fig title
fig.suptitle('set_xticklabels verticalalignment Example', fontweight='bold')
# Display
plt.show()
Here we pass the verticalalignment argument to the set_xticklabels method, to set the alignment of ticklabels to the top.
Matplotlib set_xticklabels horizontal alignemnt
Here we’ll learn to change the horizontal alignment of x ticklabels. To change the alignment pass the horizontalalignment parameter to the method.
You can write ha in place of horizontalalignment
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels, horizontalalignment='center' | 'right' | 'left' )
Let’s see examples of different horizontal alignment:
horizontalalignment=’center’
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y1 = np.tan(x)
# Plot
ax.plot(x, y1)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], horizontalalignment='center')
# Add fig title
fig.suptitle('set_xticklabels horizontalalignmnt Example', fontweight='bold')
# Display
plt.show()
- Here we use the linespace() and tan() method to define data coordinates.
- After this, we use the plot() function to plot the line graph between x and y data coordinates.
- To set the ticks, use set_xticks() method .
- To set the tick labels in string format, we use the set_xticklabels() method.
- Here we set the horizontalalignment of tick labels to the center.
horizontalalignment= ‘right’
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y = np.tan(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], horizontalalignment='right')
# Add fig title
fig.suptitle('set_xticklabels horizontalalignment Example', fontweight='bold')
# Display
plt.show()
Here we set the horizontalalignment of the x ticklabels to right.
horizontalalignment=’left’
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y = np.tan(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], horizontalalignment='left')
# Add fig title
fig.suptitle('set_xticklabels horizontalalignment Example', fontweight='bold')
# Display
plt.show()
Matplotlib set_xticklabels rotation
Here we’ll learn to rotate the x-axis ticklabels. To change the angle of rotation pass rotation argument to set_xtciklabels() method. We can use it to avoid overlapping labels at the x-axis.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels, rotation=None)
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y1 = np.exp(x)
# Plot
ax.plot(x, y1)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], rotation=30)
# Add fig title
fig.suptitle('set_xticklabels rotation Example', fontweight='bold')
# Display
plt.show()
- In the above example, to define the data coordinates we use the linespace(), and exp() method. And to plot a graph, use the plot() method of matplotlib.
- To rotate the ticklabels here we use the set_xticklabels() function with rotation argument.
Matplotlib set_xticklabels hide
Here we’ll learn how to hide ticklabels at the x-axis. Here we set the ticks labels to be empty so that it makes the axis ticks or tick labels invisible. But the ticks remain.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels([])
Let’s see an example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y = np.exp(x)
# Plot
ax.plot(x, y)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels([])
# Add fig title
fig.suptitle('set_xticklabels invisible Example', fontweight='bold')
# Display
plt.show()
Here we pass the blank list to the set_xtciklabels() method so that labels get invisible.
Matplotlib set_xticklabels alignment
Here we’ll learn to change the alignment of the x-axis ticklabels. To change the alignment pass rotation as an argument to set_xticklabels method and set’s its value to vertical and horizontal.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(label, rotation='vertical' | 'horizontal')
Examples:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 5 * np.pi, 150)
y1 = np.cos(x)
y2 = np.sin(x)
# Plot
ax.plot(x, y1)
ax.plot(x, y2)
# Set ticklabels
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], rotation='vertical')
#OR
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$', r'4$\pi$', r'5$\pi$'], rotation='horizontal')
# Add fig title
fig.suptitle('set_xticklabels alignment Example', fontweight='bold')
# Display
plt.show()
In the above example, we pass the rotation argument to the set_xticklabels() method and we set its value vertical and horizontal, to align the labels at the x-axis.
Matplotlib set_xticklabels minor
Here we’ll learn how to set the minor ticklabels rather than the major ones at the x-axis.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels, minor=True)
Example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Create subplot
fig, ax = plt.subplots()
# Define Data
x = np.linspace(0, 3 * np.pi, 150)
y1 = np.cos(x)
y2 = np.sin(x)
# Plot
ax.plot(x, y1)
ax.plot(x, y2)
# Set ticklabels
ax.set_xticks([0, np.pi, 2* np.pi, 3 * np.pi], minor=True)
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$', r'3$\pi$'], minor=True, fontsize=15, color='red')
# Add fig title
fig.suptitle('set_xticklabels minor ticks Example', fontweight='bold')
# Display
plt.show()
In the above example, we pass minor as an argument to both the method set_xticks and set_xticklabels to set the minor ticklabels and ticks.
Matplotlib boxplot set_xticklabels
We’ll learn how to draw a boxplot and set the text labels at the x-axis.
The following is the syntax:
matplotlib.axes.Axes.set_xticklabels(labels)
Example:
# Import Library
import numpy as np
import matplotlib.pyplot as plt
# Subplot
fig, ax = plt.subplots()
# Define Data Coordinates
xticks = [2, 4, 6, 8, 10]
# Plot
plt.boxplot(xticks)
# Ticklabels
ax.set_xticks(xticks)
ax.set_xticklabels(['Mon' , 'Tue' , 'Wed', 'Thur', 'Fri' ])
# Display
plt.show()
- In the above example, we import matplotlib.pyplot, and numpy.
- Then we create a subplot using subplots() method.
- After this, we plot the boxplot using the boxplot() method.
- To set the ticks and ticklabels, use the set_xticks and set_xticklabels method.
You may like the following Python Matplotlib tutorials:
- Matplotlib fill_between
- Matplotlib set_yticklabels
- Matplotlib tight_layout
- Python Matplotlib tick_params + 29 examples
- Matplotlib x-axis label
- Matplotlib multiple bar chart
In this tutorial, we learned:
- Matplotlib set_xticklabels
- Matplorlib set_xticklabels fontdict
- Matplotlib set_xticklabels fontsize
- Matplotlib set_xticklabels font
- Matplotib set_xticklabels color
- Matplotlib set_xticklabels vertical
- Matplotlib set_xticklabels horizontal alignment
- Matplotlib set_xticklabels rotation
- Matplotlib set_xticklabels hide
- Matplotlib set_xticklabels alignment
- Matplotlib set_xticklabels minor
- Matplotlib boxplot set_xticklabels
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.