Issue
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)
Solution
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)
If you want to use the OOP interface, you can achieve the same by
for label in ax.get_xticklabels():
label.update(mydict)
Answered By - Stef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.