The Python Oracle

Remove xticks in a matplotlib plot?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 Question
00:29 Accepted answer (Score 687)
01:11 Answer 2 (Score 238)
01:29 Answer 3 (Score 157)
01:47 Answer 4 (Score 89)
02:04 Thank you

--

Full question
https://stackoverflow.com/questions/1299...

Accepted answer links:
[plt.tick_params]: https://matplotlib.org/stable/api/_as_ge...
[ax.tick_params]: https://matplotlib.org/stable/api/_as_ge...

Answer 4 links:
[matplotlib mailing list]: http://matplotlib.1069221.n5.nabble.com/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #matplotlib #plot

#avk47



ACCEPTED ANSWER

Score 738


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




ANSWER 2

Score 270


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



ANSWER 3

Score 92


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




ANSWER 4

Score 58


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