90

I have the following code:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm

img = mpimg.imread("lena.jpg")

f, axarr = plt.subplots(2, 2)
axarr[0,0].imshow(img, cmap = cm.Greys_r)
axarr[0,0].set_title("Rank = 512")

rank = 128
new_img = prune_matrix(rank, img)
axarr[0,1].imshow(new_img, cmap = cm.Greys_r)
axarr[0,1].set_title("Rank = %s" %rank)

rank = 32
new_img = prune_matrix(rank, img)
axarr[1,0].imshow(new_img, cmap = cm.Greys_r)
axarr[1,0].set_title("Rank = %s" %rank)

rank = 16
new_img = prune_matrix(rank, img)
axarr[1,1].imshow(new_img, cmap = cm.Greys_r)
axarr[1,1].set_title("Rank = %s" %rank)

plt.show()

However, the result is pretty ugly because of the values on the axes:

enter image description here

How can I turn off axes values for all subplots simultaneously?

Sergey Ivanov
  • 3,381
  • 5
  • 33
  • 58
  • What's wrong with http://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces? Also, can you make your code runnable? – Veedrac Sep 16 '14 at 06:36
  • 1
    The problem that axis off makes invisible only the last subplot. – Sergey Ivanov Sep 16 '14 at 06:39

2 Answers2

157

You can turn the axes off by following the advice in Veedrac's comment (linking to here) with one small modification.

Rather than using plt.axis('off') you should use ax.axis('off') where ax is a matplotlib.axes object. To do this for your code you simple need to add axarr[0,0].axis('off') and so on for each of your subplots.

The code below shows the result (I've removed the prune_matrix part because I don't have access to that function, in the future please submit fully working code.)

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm

img = mpimg.imread("stewie.jpg")

f, axarr = plt.subplots(2, 2)
axarr[0,0].imshow(img, cmap = cm.Greys_r)
axarr[0,0].set_title("Rank = 512")
axarr[0,0].axis('off')

axarr[0,1].imshow(img, cmap = cm.Greys_r)
axarr[0,1].set_title("Rank = %s" % 128)
axarr[0,1].axis('off')

axarr[1,0].imshow(img, cmap = cm.Greys_r)
axarr[1,0].set_title("Rank = %s" % 32)
axarr[1,0].axis('off')

axarr[1,1].imshow(img, cmap = cm.Greys_r)
axarr[1,1].set_title("Rank = %s" % 16)
axarr[1,1].axis('off')

plt.show()

Stewie example

Note: To turn off only the x or y axis you can use set_visible() e.g.:

axarr[0,0].xaxis.set_visible(False) # Hide only x axis
Stefan Falk
  • 21,778
  • 41
  • 170
  • 332
Ffisegydd
  • 47,839
  • 14
  • 137
  • 116
30
import matplotlib.pyplot as plt

fig, ax = plt.subplots(2, 2)


To turn off axes for all subplots, do either:

[axi.set_axis_off() for axi in ax.ravel()]

or

map(lambda axi: axi.set_axis_off(), ax.ravel())
Nirmal
  • 1,009
  • 2
  • 12
  • 21
  • 4
    If you're using python 3, `map` is lazily evaluated so you'll have to wrap it in `list` – Milo Wielondek Apr 23 '20 at 12:49
  • I had to do ```[axi.axis_off() for axi in ax.ravel()]``` – Alex Witsil Jan 12 '21 at 00:05
  • I used to do this - using `map()` in Py2, and then list comprehension in Py3 to write loops. This is not good. It seems to save 1 line, but it's not a great saving, and if the loop is long, you're wasting memory and resources that way (unless you actually use the list). Just write `for axi in ax.ravel(): axi.set_axis_off() # noqa` (1 line, noqa is for flake8, which is good to use). Or admit that it's a loop, and press Enter after the colon. – Tomasz Gandor Jul 05 '21 at 18:29