简体   繁体   English

删除 matplotlib plot 中的 xticks?

[英]Remove xticks in a matplotlib plot?

I have a semilogx plot and I would like to remove the xticks.我有一个 semilogx plot,我想删除 xticks。 I tried:我试过了:

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?如何删除它们?

The plt.tick_params method is very useful for stuff like this. plt.tick_params方法对于这样的事情非常有用。 This code turns off major and minor ticks and removes the labels from the x-axis.此代码关闭主要和次要刻度并从 x 轴上删除标签。

Note that there is also ax.tick_params for matplotlib.axes.Axes objects.请注意, matplotlib.axes.Axes对象也有ax.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()

在此处输入图像描述

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

plt.axis('off')

Alternatively, you can pass an empty tick position and label as或者,您可以传递一个空的刻度位置和标签为

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

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

图形

There is a better, and simpler, solution than the one given by John Vinyard.有一个比 John Vinyard 给出的更好、更简单的解决方案。 Use NullLocator :使用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.希望有帮助。

Try this to remove the labels (but not the ticks):试试这个来删除标签(但不是蜱虫):

import matplotlib.pyplot as plt

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

example 例子

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

Those of you looking for a short command to switch off all ticks and labels should be fine with那些正在寻找关闭所有刻度和标签的简短命令的人应该可以

plt.tick_params(top=False, bottom=False, left=False, right=False,
                labelleft=False, labelbottom=False)

which allows type bool for respective parameters since version matplotlib>=2.1.1自版本 matplotlib>=2.1.1 以来,它允许为各个参数键入bool

For custom tick settings, the docs are helpful:对于自定义刻度设置,文档很有帮助:

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html

# 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')

Modify the following rc parameters by adding the commands to the script:通过将命令添加到脚本来修改以下 rc 参数:

plt.rcParams['xtick.bottom'] = False
plt.rcParams['xtick.labelbottom'] = False

A sample matplotlibrc file is depicted in this section of the matplotlib documentation, which lists many other parameters like changing figure size, color of figure, animation settings, etc. matplotlib 文档的这一部分描述了一个示例 matplotlibrc 文件,其中列出了许多其他参数,例如更改图形大小、图形颜色、动画设置等。

A simple solution to this problem is to set the color of the xticks to White or to whatever the background color is.此问题的一个简单解决方案是将xticks的颜色设置为白色或任何背景颜色。 This will hide the text of the xticks but not the xticks itself.这将隐藏 xticks 的文本而不是 xticks 本身。

Code Example代码示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM