Matplotlib - hiding specific ticks on x-axis

前端 未结 2 1193
孤城傲影
孤城傲影 2020-12-14 08:16

I am trying to hide the first and last x-axis tick text of my bar plot, which is \'2004\' and \'2013\'. Matplotlib automatically adds these in by default, even though my dat

相关标签:
2条回答
  • 2020-12-14 08:55

    Just adding to @DmitryNazarov's answer, in case you want just to hide the tick labels, keeping the grid lines visible, use:

    ax = plt.gca()     
    ax.axes.xaxis.set_ticklabels([])
    ax.axes.yaxis.set_ticklabels([])
    
    0 讨论(0)
  • 2020-12-14 09:06

    Please, tell me if it's not what you want.

    import sys, os
    import matplotlib.pyplot as plt
    
    path = sys.path[0]
    sizes = [(12,3,), (4,3,)]
    x =  range(20)
    
    
    for i, size in enumerate(sizes):
        fig = plt.figure(figsize = size, dpi = 80, facecolor='white',edgecolor=None,linewidth=0.0, frameon=True, subplotpars=None)
        ax = fig.add_subplot(111)
        ax.plot(x)
        plt.ylabel ('Some label')
        plt.tight_layout()
    
        make_invisible = True
        if (make_invisible):
            xticks = ax.xaxis.get_major_ticks()
            xticks[0].label1.set_visible(False)
            xticks[-1].label1.set_visible(False)
    
    plt.show()
    

    This example makes invisible first and last X-ticks. But you can easily add checking for your special ticks.

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