425

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?

edwinksl
  • 6,817
  • 4
  • 29
  • 35
Vincent
  • 53,995
  • 57
  • 182
  • 359
  • 2
    Some of the solutions didn't work for me. But just a little change of the examples here: `ax.set_xticks([], [])` and it's solved ... – TravelTrader Apr 23 '19 at 07:42

10 Answers10

648

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

adamconkey
  • 3,000
  • 4
  • 24
  • 53
John Vinyard
  • 12,047
  • 3
  • 27
  • 42
  • 127
    I appreciate how this not only answers the question but provides a template for turning several things off/on. This applies the result to both x and y axes: `plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')` – Steven C. Howell Jul 11 '15 at 04:32
  • 5
    What if it is a 3D plot? – tommy.carstensen Sep 02 '15 at 19:23
  • 57
    This is a great answer. For those looking for the oo version, `axes` has the same [`tick_params`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params) method. – Mad Physicist Aug 16 '16 at 17:13
  • 45
    In newer versions of `matplotlib`, you should replace `'on'` with `True` and `'off'` with `False`. – BallpointBen Mar 22 '18 at 23:22
  • 2
    Answer ought be updated for the OOP interface and the newer version syntax. – ifly6 Sep 30 '19 at 19:27
  • Very helpful answer, but this didn't quite work for me (mpl 3.5.1). But the following worked: plt.tick_params(labelcolor='none'). Also, turning off the bottom ticks bumps the plt.xlabel() text up, causing interference with the subplot labels. – Wesley Kitlasten May 08 '22 at 20:56
215

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')
Martin Spacek
  • 2,531
  • 1
  • 14
  • 9
144

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)
Trevor Boyd Smith
  • 16,711
  • 29
  • 116
  • 163
hashmuke
  • 2,715
  • 2
  • 17
  • 29
  • 9
    If you have an existing axis instance, say `ax`, then you could use: `ax.set_xticks([], [])` – Guilherme Salomé Mar 19 '19 at 18:56
  • 2
    @GuilhermeSalomé this now raises a warning, "Passing the minor parameter of set_xticks() positionally is deprecated since Matplotlib 3.2; the parameter will become keyword-only two minor releases later." What's the correct solution now? – Rylan Schaeffer Apr 23 '20 at 15:01
  • 6
    @RylanSchaeffer `ax.set_xticks([])` for major ticks, `ax.set_xticks([], minor=True)` for minor ticks. Equivalents with `pyplot` are `plt.xticks([])` and `plt.xticks([], minor=True)`. – Anakhand May 05 '20 at 13:08
88

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

Tom Phillips
  • 1,700
  • 15
  • 18
50

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

Hope that helps.

dmcdougall
  • 2,356
  • 1
  • 16
  • 15
  • 26
    "Better" is debatable, but +1 for the alternative. – mechanical_meat Jan 21 '13 at 18:38
  • A variant of this solution also worked for me when the goal was to only remove xticks from a "zoomed in" inset, while keeping them in the main plot. Using `axins.xaxis.set_major_locator(plt.NullLocator())`, where `axins` is the object returned by `axins = zoomed_inset_axes()` (function imported from `mpl_toolkits.axes_grid1.inset_locator`). – Dennis Soemers Aug 14 '18 at 13:06
32

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

import matplotlib.pyplot as plt

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

example

Chris Seymour
  • 79,902
  • 29
  • 153
  • 193
auraham
  • 1,637
  • 2
  • 20
  • 27
21

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([])
Amitrajit Bose
  • 528
  • 4
  • 13
9

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

hahnec
  • 412
  • 5
  • 11
4
# 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')
Marcelo Villa-Piñeros
  • 1,025
  • 2
  • 11
  • 23
2

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.

losnihciL
  • 107
  • 1
  • 5