Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plot multiple images (numpy array) with mathplotlib, defining only column size
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.figsize"] = 12,12
def plot_images(images, columns=4):
total = len(images)
nrow = ceil(total / columns)
ncol = columns
fig = plt.figure(figsize=(ncol+1, nrow+1))
gs = gridspec.GridSpec(nrow, ncol,
wspace=0.1, hspace=0.1,
top=1.-0.5/(nrow+1), bottom=0.5/(nrow+1),
left=0.5/(ncol+1), right=1-0.5/(ncol+1))
for i in range(nrow):
for j in range(ncol):
pos = i * ncol + j
if pos < total:
ax = plt.subplot(gs[i,j])
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.imshow(images[pos])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment