32

Is there a way to get rid of tick labels altogether when creating an array of subplots in Matplotlib? I am currently needing to specify each plot based on the row and column of a larger data set to which the plot corresponds. I've attempted to use the ax.set_xticks([]) and the similar y-axis command, to no avail.

I recognize that it's probably an unusual request to want to make a plot with no axis data whatsoever, but that's what I need. And I need it to automatically apply to all of the subplots in the array.

Palmetto_Girl86
  • 777
  • 2
  • 8
  • 19
  • possible duplicate of [Remove xticks in a matplot lib plot?](http://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplot-lib-plot) – Ben Aug 04 '14 at 17:38
  • @Ben, I encountered that in my search. It didn't prove helpful to my situation. I'm not sure why. I'm new enough to programming that I'm not sure if it's because I have multiple subplots, or what. – Palmetto_Girl86 Aug 04 '14 at 20:02
  • Instead of suggesting the status of my question as a duplicate, does anyone have suggestions of things to try? (Sorry to sound frustrated, but posting on here is a bit of a Hail Mary at this point.) – Palmetto_Girl86 Aug 04 '14 at 20:03
  • Possible duplicate of [Hiding axis text in matplotlib plots](https://stackoverflow.com/questions/2176424/hiding-axis-text-in-matplotlib-plots) – Saullo G. P. Castro Jun 04 '17 at 10:54

4 Answers4

45

You have the right method. Maybe you are not applying the set_xticks to the correct axes.

An example:

import matplotlib.pyplot as plt
import numpy as np

ncols = 5
nrows = 3

# create the plots
fig = plt.figure()
axes = [ fig.add_subplot(nrows, ncols, r * ncols + c) for r in range(0, nrows) for c in range(0, ncols) ]

# add some data
for ax in axes:
    ax.plot(np.random.random(10), np.random.random(10), '.')

# remove the x and y ticks
for ax in axes:
    ax.set_xticks([])
    ax.set_yticks([])

This gives:

enter image description here

Note that each axis instance is stored in a list (axes) and then they can be easily manipulated. As usual, there are several ways of doing this, this is just an example.

DrV
  • 19,376
  • 4
  • 53
  • 67
  • 12
    A one-liner would be `plt.setp(axes, xticks=[], yticks=[])`. – mwaskom Aug 04 '14 at 20:57
  • You can also use the `NullFormatter` – tacaswell Aug 04 '14 at 22:23
  • I think some combination of these responses will work for me! Thanks very much. +1 for the two detailed responses with testable code. :) – Palmetto_Girl86 Aug 05 '14 at 15:33
  • 1
    Edit: In order to make sure this answer runs without errors, I believe you should add in a `+1` when indexing the subplot creation: `axes = [ fig.add_subplot(nrows, ncols, r * ncols + c + 1) for r in range(0, nrows) for c in range(0, ncols) ]` – user8188120 Apr 10 '19 at 09:05
  • I'm not sure as of which version of matplotlib, but the simple for loop over the axes will no longer work as they are now stored as a two-d array if columns and rows are both not 1. Just something to keep in mind. – L. IJspeert Sep 25 '20 at 10:23
18

The commands are the same for subplots

fig = plt.figure()

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.plot([1,2])

ax1.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off'  # labels along the bottom edge are off)
)

plt.draw()

enter image description here

Opsse
  • 1,677
  • 2
  • 20
  • 35
Ben
  • 5,979
  • 5
  • 32
  • 63
  • This is better than the accepted answer as it keeps any grid you have. The accepted answer will also delete any x-grid you use. eg. when using style ggplot – Florida Man Nov 07 '18 at 23:05
16

Even more concise than @DrV 's answer, remixing @mwaskom's comment, a complete and total one-liner to get rid of all axes in all subplots:

# do some plotting...
plt.subplot(121),plt.imshow(image1)
plt.subplot(122),plt.imshow(image2)
# ....

# one liner to remove *all axes in all subplots*
plt.setp(plt.gcf().get_axes(), xticks=[], yticks=[]);
Roy Shilkrot
  • 1,603
  • 19
  • 15
  • 1
    @Roy This option is amazing but it also removes the ticks. Is it possible to remove only the labels and keep the ticks? (I have too many subplots so it would be great if there is an option to fix this without the need to go again through each subplot – Zeineb Jul 26 '20 at 13:16
0

You can get rid of the default subplot x and y ticks with simply running the following codes:

fig, ax = plt.subplots()
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for i in range(3):
    ax = fig.add_subplot(3, 1, i+1)
    ...

Just by adding the 2 aforementioned lines just after fig, ax = plt.subplots() you can remove the default ticks.

MMG
  • 2,962
  • 5
  • 8
  • 35