首页 文章

删除matplotlib图中的xticks?

提问于
浏览
188

我有一个semilogx情节,我想删除xticks . 我试过了:

plt.gca().set_xticks([])
plt.xticks([])
ax.set_xticks([])

网格消失(确定),但小蜱(在主蜱的位置)仍然存在 . 如何删除它们?

7 回答

  • 321

    试试这个删除标签(但不是刻度线):

    import matplotlib.pyplot as plt
    
    plt.setp( ax.get_xticklabels(), visible=False)
    

    example

  • 4

    此代码段可能有助于仅删除xticks .

    from matplotlib import pyplot as plt    
    plt.xticks([])
    

    这段代码可能有助于删除xticks和yticks .

    from matplotlib import pyplot as plt    
    plt.xticks([]),plt.yticks([])
    
  • 22

    tick_params方法对于这样的东西非常有用 . 此代码关闭主要和次要刻度,并从x轴中删除标签 .

    from matplotlib import pyplot as plt
    plt.plot(range(10))
    plt.tick_params(
        axis='x',          # changes apply to the x-axis
        which='both',      # both major and minor ticks are affected
        bottom=False,      # ticks along the bottom edge are off
        top=False,         # ticks along the top edge are off
        labelbottom=False) # labels along the bottom edge are off
    plt.show()
    plt.savefig('plot')
    plt.clf()
    

    enter image description here

  • 39

    不完全是OP所要求的,但是禁用所有轴线,刻度线和标签的简单方法是简单地调用:

    plt.axis('off')
    
  • 72

    这是我在matplotlib mailing list上找到的替代解决方案:

    import matplotlib.pylab as plt
    
    x = range(1000)
    ax = plt.axes()
    ax.semilogx(x, x)
    ax.xaxis.set_ticks_position('none')
    

    graph

  • 46

    或者,您可以传递空的刻度位置并标记为

    plt.xticks([], [])
    
  • 50

    有一个比John Vinyard给出的更好,更简单的解决方案 . 使用 NullLocator

    import matplotlib.pyplot as plt
    
    plt.plot(range(10))
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.show()
    plt.savefig('plot')
    

    希望有所帮助 .

相关问题