Reputation: 3929
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")
fig, axs = plt.subplots(2, 2)
axs[0,0].imshow(img, cmap = cm.Greys_r)
axs[0,0].set_title("Rank = 512")
rank = 128
new_img = prune_matrix(rank, img)
axs[0,1].imshow(new_img, cmap = cm.Greys_r)
axs[0,1].set_title("Rank = %s" %rank)
rank = 32
new_img = prune_matrix(rank, img)
axs[1,0].imshow(new_img, cmap = cm.Greys_r)
axs[1,0].set_title("Rank = %s" %rank)
rank = 16
new_img = prune_matrix(rank, img)
axs[1,1].imshow(new_img, cmap = cm.Greys_r)
axs[1,1].set_title("Rank = %s" %rank)
plt.show()
However, the result is pretty ugly because of the values on the axes:
How can I turn off axes values for all subplots simultaneously?
How to remove axis, legends, and white padding doesn't work because I don't know how to make it work with subplots.
Upvotes: 115
Views: 249786
Reputation: 53678
Axes
off by following the advice in Veedrac's comment (linking to here) with one small modification.plt.axis('off')
, use ax.axis('off')
where ax
is a matplotlib.axes
object.
Axes
, axs[0, 0].axis('off')
, and so on for each subplot.pyplot
and Axes
.prune_matrix
, which is not available.import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
import matplotlib.cbook as cbook # used for matplotlib sample image
# load readily available sample image
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
img = plt.imread(image_file)
# read a local file
# img = mpimg.imread("file.jpg")
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)
axs[0, 0].imshow(img, cmap=cm.Greys_r)
axs[0, 0].set_title("Rank = 512")
axs[0, 0].axis("off")
axs[0, 1].imshow(img, cmap=cm.Greys_r)
axs[0, 1].set_title("Rank = %s" % 128)
axs[0, 1].axis("off")
axs[1, 0].imshow(img, cmap=cm.Greys_r)
axs[1, 0].set_title("Rank = %s" % 32)
axs[1, 0].axis("off")
axs[1, 1].imshow(img, cmap=cm.Greys_r)
axs[1, 1].set_title("Rank = %s" % 16)
axs[1, 1].axis("off")
plt.show()
Note: To turn off only the x or y axis you can use set_visible()
e.g.:
axs[0, 0].xaxis.set_visible(False) # Hide only x axis
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)
# convert the 2d array to 1d, which removes the need to iterate through i and j
axs = axs.flat
ranks = [512, 128, 32, 16]
# iterate through each Axes with the associate rank
for ax, rank in zip(axs, ranks):
ax.imshow(img, cmap=cm.Greys_r)
ax.set_title(f'Rank = {rank}')
ax.axis('off')
plt.show()
Upvotes: 200
Reputation: 23021
Another possible way is to set the axison
attribute to False for each Axes as they get plotted.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread("Stewie_Griffin.png")
fig, axs = plt.subplots(2, 2)
for i, lst in enumerate([[512, 128], [32, 16]]):
for j, rank in enumerate(lst):
axs[i,j].imshow(img)
axs[i,j].set_title(f"Rank = {rank}")
axs[i,j].axison = False # <---- remove axis
If you want to clearly see what is removed, you can "remove" frames and ticks separately using Axes.set()
.
axs[i,j].set(frame_on=False, xticks=[], yticks=[])
Finally, if you want to remove the frames and ticks after the graphs are plotted, you can loop over the list of axes in the figure itself.
for ax in fig.axes:
ax.axison = False
N.B. All three methods given here (axis('off')
, set_axis_off()
and axison=False
) are equivalent methods because under the hood, axis('off')
calls set_axis_off()
, which in turns does axison=False
, so ultimately, they are the same.
Upvotes: 1
Reputation: 1435
Given:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
To turn off axes for all subplots:
for ax in axs.ravel():
ax.set_axis_off()
Upvotes: 39