Python 删除 matplotlib 图中的 xticks?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12998430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove xticks in a matplotlib plot?
提问by Vincent
I have a semilogx plot and I would like to remove the xticks. I tried:
我有一个 semilogx 图,我想删除 xticks。我试过:
plt.gca().set_xticks([])
plt.xticks([])
ax.set_xticks([])
The grid disappears (ok), but small ticks (at the place of the main ticks) remain. How to remove them?
网格消失(确定),但小刻度(在主要刻度的位置)仍然存在。如何删除它们?
采纳答案by John Vinyard
The tick_paramsmethod is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis.
该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()


回答by dmcdougall
There is a better, and simpler, solution than the one given by John Vinyard. Use NullLocator:
有一种比 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')
Hope that helps.
希望有帮助。
回答by auraham
回答by Martin Spacek
Not exactly what the OP was asking for, but a simple way to disable all axes lines, ticks and labels is to simply call:
不完全是 OP 所要求的,但是禁用所有轴线、刻度和标签的简单方法是简单地调用:
plt.axis('off')
回答by Tom Phillips
Here is an alternative solution that I found on the matplotlib mailing list:
这是我在matplotlib 邮件列表中找到的替代解决方案:
import matplotlib.pylab as plt
x = range(1000)
ax = plt.axes()
ax.semilogx(x, x)
ax.xaxis.set_ticks_position('none')


回答by hashmuke
Alternatively, you can pass an empty tick position and label as
或者,您可以传递一个空的刻度位置并标记为
plt.xticks([], [])
回答by Amitrajit Bose
This snippet might help in removing the xticks only.
此代码段可能仅有助于删除 xticks。
from matplotlib import pyplot as plt
plt.xticks([])
This snippet might help in removing the xticks and yticks both.
此代码段可能有助于删除 xticks 和 yticks。
from matplotlib import pyplot as plt
plt.xticks([]),plt.yticks([])
回答by Nishant Wattamwar
# 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')

