How to set my xlabel at the end of xaxis

前端 未结 4 1715
渐次进展
渐次进展 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:22

    Here is my variant of using @JoeKington method. I change last tick label to axis name. First I set last ticks to empty strings and then use annotate(). I've used annotate() because I need to control font size of axis label.

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.xlim(50, 70)
    plt.ylim(100, 250)
    
    ax = plt.gca()
    # clears last tick label
    xticks = ax.get_xticks().tolist()
    xticks[-1] = ''
    ax.set_xticklabels(xticks)
    yticks = ax.get_yticks().tolist()
    yticks[-1] = ''
    ax.set_yticklabels(yticks)
    # sets axes labels on both ends
    ax.annotate('$t$', xy=(0.98, 0), ha='left', va='top', xycoords='axes fraction', fontsize=20)
    ax.annotate('$x$', xy=(0, 1), xytext=(-15,2), ha='left', va='top', xycoords='axes fraction', textcoords='offset points', fontsize=20)
    
    plt.show(block=True)
    

    Maybe someone knows more elegant way to do this, because it is ridiculously complex operation.

    enter image description here

提交回复
热议问题