2

I was trying to plot two images side by side without any junk like grid lines and axes. I found that you can turn off ALL grid lines with plt.rcParams['axes.grid'] = False, but can't figure out if there's a similar option for axes. I know you can use plt.axis('off') but then you'd have to specify it for each subplot individually.

plt.rcParams['axes.grid'] = False

plt.subplot(1, 2, 1)
plt.imshow(img1)
plt.subplot(1, 2, 2)
plt.imshow(img2)

plt.show()
Raksha
  • 1,572
  • 2
  • 28
  • 53

3 Answers3

5

The different components of the axes all have their individual rc parameter. So to turn "everything off", you would need to set them all to False.

import numpy as np
import matplotlib.pyplot as plt

rc = {"axes.spines.left" : False,
      "axes.spines.right" : False,
      "axes.spines.bottom" : False,
      "axes.spines.top" : False,
      "xtick.bottom" : False,
      "xtick.labelbottom" : False,
      "ytick.labelleft" : False,
      "ytick.left" : False}
plt.rcParams.update(rc)

img = np.random.randn(100).reshape(10,10)

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

ax.imshow(img)
ax2.imshow(img)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

One option is to loop through the axes on the figure and turn them off. You need to create the figure object and then use fig.axes which returns a list of subplots :

img = np.random.randn(100).reshape(10,10)

fig = plt.figure()

plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(img)

for ax in fig.axes:
    ax.axis("off")

enter image description here

You could also go through the rcParams and set all the spines, ticks, and tick labels to False.

DavidG
  • 24,279
  • 14
  • 89
  • 82
1

Try using:

plt.axis('off')

And:

plt.grid(False)

Instead, whole code would be:

plt.subplot(1, 2, 1)
plt.imshow(img1)
plt.axis('off')
plt.grid(False)
plt.subplot(1, 2, 2)
plt.imshow(img2)
plt.axis('off')
plt.grid(False)

plt.show()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 2
    That doesn't work. `plt.axis('off')` has to go after the line where subplot is created, and it only applies to that subplot – Raksha Dec 11 '18 at 02:13