删除matplotlib图中的xticks?

问题描述 投票:217回答:8

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

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

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

python matplotlib plot
8个回答
379
投票

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()


97
投票

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

plt.axis('off')

59
投票

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

import matplotlib.pylab as plt

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


58
投票

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

plt.xticks([], [])

39
投票

有一个比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')

希望有所帮助。


24
投票

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

import matplotlib.pyplot as plt

plt.setp( ax.get_xticklabels(), visible=False)

example


9
投票

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

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

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

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

2
投票
# remove all the ticks (both axes), and tick labels on the Y axis
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
© www.soinside.com 2019 - 2024. All rights reserved.