Rotate minor ticks in matplotlib

后端 未结 3 912
遇见更好的自我
遇见更好的自我 2020-12-11 02:52

I am plotting the following chart :

with the following code:

fig, ax = plt.subplots(figsize=(20, 3))
mpf.candlestick_ohlc(ax,quotes, width=0         


        
相关标签:
3条回答
  • 2020-12-11 03:34

    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.

    0 讨论(0)
  • 2020-12-11 03:36

    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)
    

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

    0 讨论(0)
  • 2020-12-11 03:46

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

    0 讨论(0)
提交回复
热议问题