Set the tick labels in Matplotlib

Python

Matplotlib

Axes

Set tick labels

Luc B.

Python

Matplotlib

Matplotlib is quite good at choosing the best tick mark spacing and labels for your visualization. However, visualizations often benefit from manually chosen tick marks that highlight certain features of the data.

Code Example

Use the plt.xticks() and plt.yticks() functions to set the tick labels in Matplotlib.

import matplotlib.pyplot as plt

# Set the tick labels here
plt.xticks([1, 2, 3, 5], ["One", "Two", "Three", "Five"])
plt.yticks([0, 100, 1000])

plt.show()

png

The first argument to plt.xticks() and plt.yticks() is a numerical list of tick mark positions. The optional second argument is a list of text labels for each tick mark. By default, tick marks are labeled with their numerical position.

More Examples

Object Oriented Interface

Matplotlib's object oriented interface implements the Axes.set_xticks(), Axes.set_xticklabels(), Axes.set_yticks(), and Axes.set_yticklabels() functions to customize the tick labels.

Unlike the pyplot interface, the object oriented interface implements separate functions for setting the tick mark positions and labels.

ax = plt.axes()

# Set the x tick positions and labels
ax.set_xticks([1, 2, 3, 5])
ax.set_xticklabels(["One", "Two", "Three", "Five"])

# Set the y tick positions, use default numerical labels
ax.set_yticks([0, 100, 1000])

plt.show()

png