删除matplotlib图中的xticks?

时间:2012-10-21 13:53:56

标签: python matplotlib plot

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

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

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

10 个答案:

答案 0 :(得分:368)

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

enter image description here

答案 1 :(得分:94)

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

plt.axis('off')

答案 2 :(得分:57)

以下是我在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

答案 3 :(得分:57)

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

plt.xticks([], [])

答案 4 :(得分: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')

希望有所帮助。

答案 5 :(得分:23)

尝试此操作以删除标签(但不是刻度线):

import matplotlib.pyplot as plt

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

example

答案 6 :(得分:8)

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

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

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

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

答案 7 :(得分:3)

您正在寻找一个简短的命令来关闭所有刻度线和标签的人应该很好用

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

由于版本matplotlib> = 2.1.1

,允许为各个参数输入bool

对于自定义刻度线设置,文档非常有用:

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

答案 8 :(得分:1)

20190418
20181023
20181123
20181223
20181201
20181209
20181210
20181229
20181231
00000000
00000000
00000000

答案 9 :(得分:1)

通过将命令添加到脚本来修改以下rc参数:

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

matplotlib文档的this section中描述了一个示例matplotlibrc文件,其中列出了许多其他参数,如更改图形大小,图形颜色,动画设置等。