48

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)

How can I retain the grid while makeing x-axis ticks invisible?

DurgaDatta
  • 3,952
  • 4
  • 27
  • 34

1 Answers1

79

By removing 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

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 tick in ax.xaxis.get_major_ticks():
    tick.tick1line.set_visible(False)
    tick.tick2line.set_visible(False)
    tick.label1.set_visible(False)
    tick.label2.set_visible(False)

plt.show()
Parham ZM
  • 3
  • 3
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Ya, this is what I wanted. I just wanted to remove the tick labels. – DurgaDatta Dec 06 '13 at 05:35
  • 2
    Oh cool. Well, just in case, I also figured out how to remove the little tick lines too :) – mgilson Dec 06 '13 at 05:36
  • I have two subplots (ax1 and ax2). How can I make the change without changing the individual axes attribute? I mean, is there a way to do something like fig.setxticklables([]) than doing it for axes, such that both ax1 and ax2 get the changes? – DurgaDatta Dec 06 '13 at 06:17
  • @DurgaDatta -- Not that I know of (although there are people around more knowledgeable of these things than I am). I think you could do something like `for ax in fig.get_axes(): ax.set_xticklabels([])` after you've done the plotting... Does that help? – mgilson Dec 06 '13 at 06:31
  • I get my work done, but I am trying to learn how to set the attributes at once for subplots. – DurgaDatta Dec 06 '13 at 06:37
  • @DurgaDatta @mgnilson, if `axs` is your array of subplots you can modify their properties 'at once' with `plt.setp(axs, xticklabels=[])` – Rutger Kassies Dec 06 '13 at 08:41
  • How to this directly from the `plt`, i.e., when you don't do `ax = fig.add_subplot(111)`? – pceccon Apr 07 '16 at 19:50
  • 1
    @pceccon -- I haven't really used `matplotlib` much for a while, but perhaps `ax = plt.gca()`? – mgilson Apr 07 '16 at 19:51
  • 11
    I would like to complement @mgilson answer with some recent insight. As of early 2020, the recommended way to hide the tick lines is not ```tic.tick1On=False``` anymore. The correct way is now: ```Tick.tick1line.set_visible(False)```. – Pierre Massé Apr 24 '20 at 13:33