Matplotlib Subplots -- Get Rid of Tick Labels Altogether

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.

Answers


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:

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.


The commands are the same for subplots

In [1]: fig = plt.figure()

In [2]: ax1 = fig.add_subplot(211)

In [3]: ax2 = fig.add_subplot(212)

In [4]: ax1.plot([1,2])
Out[4]: [<matplotlib.lines.Line2D at 0x10ce9e410>]

In [5]: 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)
   ....:)

In [6]: plt.draw()


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=[]);

Need Your Help

Correct Putty configuration for Ubuntu 9.04

ubuntu putty

I often use Putty (http://www.chiark.greenend.org.uk/~sgtatham/putty/) to connect to Ubuntu boxes (9.04 now). It works well, but I can't figure out the correct combination of Putty font, character

decodeURIComponent vs unescape, what is wrong with unescape?

javascript encoding escaping dhtml

In answering another question I became aware that my Javascript/DOM knowledge had become a bit out of date in that I am still using escape/unescape to encode the contents of URL components whereas it