Rotate axis text in python matplotlib

前端 未结 13 898
暗喜
暗喜 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:49

    It will depend on what are you plotting.

    import matplotlib.pyplot as plt
    
     x=['long_text_for_a_label_a',
        'long_text_for_a_label_b',
        'long_text_for_a_label_c']
    y=[1,2,3]
    myplot = plt.plot(x,y)
    for item in myplot.axes.get_xticklabels():
        item.set_rotation(90)
    

    For pandas and seaborn that give you an Axes object:

    df = pd.DataFrame(x,y)
    #pandas
    myplot = df.plot.bar()
    #seaborn 
    myplotsns =sns.barplot(y='0',  x=df.index, data=df)
    # you can get xticklabels without .axes cause the object are already a 
    # isntance of it
    for item in myplot.get_xticklabels():
        item.set_rotation(90)
    

    If you need to rotate labels you may need change the font size too, you can use font_scale=1.0 to do that.

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