Rotate minor ticks in matplotlib

You may rotate by code of one line plt.setp(ax.xaxis.get_minorticklabels(), rotation=90).


By exploring a little, I discovered that ax.get_xminorticklabels() is a list with a text class element.

>>> print(type(ax.get_xminorticklabels()[0])) 
<class 'matplotlib.text.Text'>

And text can be rotated!

>>> for text in ax.get_xminorticklabels():
>>>     text.set_rotation(90)

xminorticklabels_rotate_example

You only have to be careful that they do not overlap.


While dealing with the problem myself, I discovered that you can also easily accomplish this with a single statement using the tick_params:

ax.tick_params(axis="x", which="both", rotation=45)

This will rotate labels on your x axis, and the which option allows you to choose between minor, major or both. In case you have multiple plots you will have to do this for every plot in the figure.