Search code examples
pythonmatplotlibaxes

How to make the last mark invisible in a matplotlib axes subplot?


Say the marks of an axes subplot is shown as "0,20,40,60,80,100", but I want to make the last mark 100 invisible. Not sure how to do it for a matplotlib axes subplot?


Solution

  • If you are setting the positions of x-ticks, you can simply replace the last label by a empty string.

    import pylab as plt
    
    x=[12, 13, 14, 15, 16]
    y=[14, 15, 16, 17, 18]
    
    xticks=[12,13,14,15,16]
    
    fig=plt.figure()
    ax=fig.add_subplot(111)
    ax.plot(x,y,'o-')
    
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticks[:-1]+[""])
    plt.show()
    

    You can also use the following to remove the last x-tick label

    for label in ax.get_xticklabels():
        pass
    label.set_visible(False)