如何从图中删除Xticks

发布于 2025-02-06 09:09:27 字数 168 浏览 3 评论 0 原文

我有一个Semilogx情节,我想删除Xticks。我尝试了:

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

网格消失(可以),但是小tick(在主壁虱的位置)仍然存在。如何删除它们?

I have a semilogx plot and I would like to remove the 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

岁月打碎记忆 2025-02-13 09:09:27

请注意,还有 ax.tick_params 对于 matplotlib.axes.axes.axes 对象。

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

“在此处输入图像说明”

The plt.tick_params method is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis.

Note that there is also ax.tick_params for matplotlib.axes.Axes objects.

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

掐死时间 2025-02-13 09:09:27

并不是完全要求OP要求的,而是一种禁用所有轴线,tick和标签的简单方法就是简单地致电:

plt.axis('off')

Not exactly what the OP was asking for, but a simple way to disable all axes lines, ticks and labels is to simply call:

plt.axis('off')
空城之時有危險 2025-02-13 09:09:27

另外,您可以将空的刻度位置传递给

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

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)
挽心 2025-02-13 09:09:27

这是我在

import matplotlib.pylab as plt

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

“

Here is an alternative solution that I found on the 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

雨轻弹 2025-02-13 09:09:27

有一个比约翰·维尼德(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')

There is a better, and simpler, solution than the one given by John Vinyard. Use NullLocator:

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.show()
plt.savefig('plot')
浪漫之都 2025-02-13 09:09:27

尝试删除标签(而不是tick):

import matplotlib.pyplot as plt

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

example

Try this to remove the labels (but not the ticks):

import matplotlib.pyplot as plt

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

example

听不够的曲调 2025-02-13 09:09:27

该片段可能有助于删除Xticks。

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

该片段可能有助于消除Xticks和yticks。

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

This snippet might help in removing the xticks only.

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

This snippet might help in removing the xticks and yticks both.

from matplotlib import pyplot as plt    
plt.xticks([]),plt.yticks([])
一袭白衣梦中忆 2025-02-13 09:09:27

你们中的那些正在寻找一个简短命令来关闭所有tick和标签的

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

那些应该可以使用 bool 用于各自参数的类型,因为版本matplotlib> = 2.1.1

对于自定义滴答设置,文档很有帮助。 :

httpps://matplotlib.orgg/apib.orpi/api/_as_as_gen/matplib.axes-.arplib.axes..ar> https:/ axes.tick_params.html

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

For custom tick settings, the docs are helpful:

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

逐鹿 2025-02-13 09:09:27

可以简单地更改可见性:

ax.xaxis.set_visible(false)

It is possible to simply change the visibility:

ax.xaxis.set_visible(False)

眉目亦如画i 2025-02-13 09:09:27
# 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')
# 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')
泼猴你往哪里跑 2025-02-13 09:09:27

Modify the following rc parameters by adding the commands to the script:

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

A sample matplotlibrc file is depicted in

Modify the following rc parameters by adding the commands to the script:

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.

陪你搞怪i 2025-02-13 09:09:27

如果您想删除tick标签,但要保留tick和网格,则此代码段将起作用。

plt.gca().axes.xaxis.set_ticklabels([])
plt.gca().axes.yaxis.set_ticklabels([])
plt.grid(alpha = 0.2)

If you want to remove tick labels but keep the ticks and grids, this code snippet will work.

plt.gca().axes.xaxis.set_ticklabels([])
plt.gca().axes.yaxis.set_ticklabels([])
plt.grid(alpha = 0.2)

enter image description here

痴意少年 2025-02-13 09:09:27

解决此问题的一个简单解决方案是将 Xticks 的颜色设置为白色或背景颜色的任何内容。这将隐藏Xticks的文字,而不是Xticks本身。

import matplotlib.pyplot as plt
plt.plot()
plt.xticks(color='white')
plt.show()

结果

A simple solution to this problem is to set the color of the xticks to White or to whatever the background color is. This will hide the text of the xticks but not the xticks itself.

import matplotlib.pyplot as plt
plt.plot()
plt.xticks(color='white')
plt.show()

Result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文