How to set my xlabel at the end of xaxis

前端 未结 4 1738
渐次进展
渐次进展 2020-12-24 05:42

I want my x axis has the label like this format

0 1 2 3 4 5 Xlabel

but I try code below it result me in 2 lines

self.axes.         


        
4条回答
  •  孤城傲影
    2020-12-24 06:26

    I use the solution provided by @JoeKington, with the extension of having label font properties imposed on the text produced by ax.annotate(...).

    This is important when axis labels are formatted differently than other text.

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    ticklabelpad = mpl.rcParams['xtick.major.pad']
    
    fig, ax = plt.subplots()
    ax.set_xlim([0, 5])
    
    dx_in_points = 5
    fontproperties = ax.xaxis.get_label().get_fontproperties()
    
    ax.annotate('XLabel', xy=(1,0), xytext=(dx_in_points, -ticklabelpad), ha='left', va='top',
             xycoords='axes fraction', textcoords='offset points', fontproperties=fontproperties)
    
    plt.show()
    

提交回复
热议问题