我有一个半对数图,我想去除xtick。我试着:
plt.gca().set_xticks([])
plt.xticks([])
ax.set_xticks([])
网格消失了(ok),但是小扁虱(在主要扁虱的地方)仍然存在。如何去除?
我有一个半对数图,我想去除xtick。我试着:
plt.gca().set_xticks([])
plt.xticks([])
ax.set_xticks([])
网格消失了(ok),但是小扁虱(在主要扁虱的地方)仍然存在。如何去除?
当前回答
不完全是OP要求的,但是一个简单的方法禁用所有轴、线、勾和标签是简单地调用:
plt.axis('off')
其他回答
plt。Tick_params方法对于这样的东西非常有用。这段代码关闭大刻度和小刻度,并从x轴上删除标签。
注意这里还有ax。matplotlib.axes.Axes对象的tick_params。
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()
这个问题的一个简单解决方案是将xtick的颜色设置为白色或任何背景色。这将隐藏xticks的文本,但不隐藏xticks本身。
import matplotlib.pyplot as plt
plt.plot()
plt.xticks(color='white')
plt.show()
结果
有一个比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')
或者,您可以传递一个空的标记位置并将其标记为
# for matplotlib.pyplot
# ---------------------
plt.xticks([], [])
# for axis object
# ---------------
# from Anakhand May 5 at 13:08
# for major ticks
ax.set_xticks([])
# for minor ticks
ax.set_xticks([], minor=True)
# 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')