Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 662
Raksha Avatar asked Dec 06 '25 23:12

Raksha


2 Answers

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

like image 66
ImportanceOfBeingErnest Avatar answered Dec 08 '25 14:12

ImportanceOfBeingErnest


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.

like image 28
DavidG Avatar answered Dec 08 '25 15:12

DavidG