Categories
Python Answers

How to modify tick label text with Python matplotlib?

Spread the love

Sometimes, we want to modify tick label text with Python matplotlib.

In this article, we’ll look at how to modify tick label text with Python matplotlib.

How to modify tick label text with Python matplotlib?

To modify tick label text with Python matplotlib, we can call set_xticklabels.

For instance, we write

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fig.canvas.draw()

labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = 'Testing'

ax.set_xticklabels(labels)

plt.show()

to create the labels with [item.get_text() for item in ax.get_xticklabels()].

Then we call ax.set_xticklabels to set labels as the label values for the x-axis.

Conclusion

To modify tick label text with Python matplotlib, we can call set_xticklabels.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *