Issue
Given an axes object in matplotlib, I want to find properties of tick labels, such as font size, rotation, etc.
Why do I want to do this? Because I an trying to write a function that adds some text to a plot, and I want the text to look the same as the (y) axis tick labels by default.
I googled and found lots of explanations about how to set tick label properties, but not how to retrieve these properties from the axes.
EDIT: Axes.get_xticklabels
works in most cases, but of course it breaks if there are currently no ticks, which is rare, but still, this is not ideal. It seems that there is a property on the Axes
object itself, not just the Text
objects returned by this function. Consider:
fig, ax = plt.subplots(1,1)
ax.yaxis.set_ticks([])
ax.yaxis.get_ticklabels() # this returns []
ax.tick_params(axis='y', labelsize=25)
ax.yaxis.set_ticks([0.5])
After running this code, the tick label "0.5" shows up in 25 point font, which means that the 25 is stored somewhere by the axes and not just in the text objects.
Solution
You probably have to get the properties from the individual matplotlib.text.Text
objects that make up the ticklabels.
For example, if you know they're all the same:
xta = ax.get_xticklabels()
xtext = xta[0]
print(xtext.get_font()) # sans\-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=10.0
print(xtext.get_fontfamily()) # ['sans-serif']
print(xtext.get_fontsize()) # 10.0
print(xtext.get_rotation()) # 0.0
print(xtext.get_rotation_mode()) # None
The getters are listed in the linked docs or can be found buried in print(dir(xtext))
Answered By - jedwards
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.