在保留网格的同时删除x轴刻度线(matplotlib) [英] Remove the x-axis ticks while keeping the grids (matplotlib)
问题描述
我想删除x轴上的刻度线,但保持垂直网格.当我执行以下操作时,我同时失去了x轴刻度和网格.
I want to remove the ticks on the x-axis but keep the vertical girds. When I do the following I lose both x-axis ticks as well as the grid.
import matplotlib.pyplot as plt
fig = plt.figure()
figr = fig.add_subplot(211)
...
figr.axes.get_xaxis().set_visible(False)
figr.xaxsis.grid(True)
在使x轴刻度线不可见的同时如何保留网格?
How can I retain the grid while makeing x-axis ticks invisible?
推荐答案
通过删除刻度线,您是说删除刻度线标签还是刻度线本身?这将删除标签:
By remove the ticks, do you mean remove the tick labels or the ticks themselves? This will remove the labels:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x))
ax.grid(True)
ax.set_xticklabels([])
plt.show()
如果您真的想摆脱那些小的刻度线,可以添加以下内容:
If you really want to get rid of the little tick lines, you can add this:
for tic in ax.xaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
您可以关闭对勾标签 a>此处也无需通过设置tic.label1On = tic.label2On = False
来求助于ax.set_xticklabels([])
"hack":
You could turn the tick labels off here too without resorting to the ax.set_xticklabels([])
"hack" by setting tic.label1On = tic.label2On = False
:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x))
ax.grid(True)
for tic in ax.xaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
tic.label1On = tic.label2On = False
plt.show()
这篇关于在保留网格的同时删除x轴刻度线(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!