Formatting the font of ticklabels in matplotlib

Viewed 34

I have a font dictionary with which I would like to format the ticklabels in a matplotlib plot.

The Axes.set_xticklabels() function comes with a warning that the method should only be used after fixing the tick positions using Axes.set_xticks. To set the ticks, I need to give them positions. I want to use the default positions and numerical formatting provided by matplotlib, and only specify the way in which the text is to be formatted.

How can I do this?

My initial attempt

ax.set_xticklabels(ax.get_xticks(), fontdict=mydict)

works, vis a vis the font formatting, but the ticklabels don't change on zooming, and the numerical formatting changed (eg originally 10, afterwards 10.0)

1 Answers

The easiest way is to use pyplot xticks with no ticks or labels:

import matplotlib.pyplot as plt

mydict = {'fontweight': 'bold'}

fig, ax = plt.subplots()
plt.xticks(**mydict)

enter image description here

If you want to use the OOP interface, you can achieve the same by

for label in ax.get_xticklabels():
    label.update(mydict)
Related