Rotate axis text in python matplotlib

前端 未结 13 920
暗喜
暗喜 2020-11-22 14:50

I can\'t figure out how to rotate the text on the X Axis. Its a time stamp, so as the number of samples increase, they get closer and closer until they overlap. I\'d like

13条回答
  •  花落未央
    2020-11-22 15:48

    Easy way

    As described here, there is an existing method in the matplotlib.pyplot figure class that automatically rotates dates appropriately for you figure.

    You can call it after you plot your data (i.e.ax.plot(dates,ydata) :

    fig.autofmt_xdate()
    

    If you need to format the labels further, checkout the above link.

    Non-datetime objects

    As per languitar's comment, the method I suggested for non-datetime xticks would not update correctly when zooming, etc. If it's not a datetime object used as your x-axis data, you should follow Tommy's answer:

    for tick in ax.get_xticklabels():
        tick.set_rotation(45)
    

提交回复
热议问题