Raksha
Raksha

Reputation: 1699

Is there a way to hide axes for all subplots?

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()

Upvotes: 2

Views: 3450

Answers (3)

DavidG
DavidG

Reputation: 25362

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.

Upvotes: 3

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

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

Upvotes: 6

U13-Forward
U13-Forward

Reputation: 71580

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()

Upvotes: 1

Related Questions