如何移除matplotlib图表中的xticks?

2024-04-25 07:41:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个semilogx图,我想删除xticks。我试过:

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

网格将消失(确定),但仍保留小记号(在主记号处)。如何移除它们?


Tags: 网格pltaxset消失xticks记号gca
3条回答

不完全是OP所要求的,但是一个简单的方法来禁用所有轴线、记号和标签,就是简单地调用:

plt.axis('off')

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

^{}方法对于这种情况非常有用。此代码关闭主刻度和次刻度,并从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

相关问题 更多 >