Skip to content

Instantly share code, notes, and snippets.

@emrahgunduz
Created April 15, 2021 11:25
Show Gist options
  • Save emrahgunduz/2f7ae873175699d8c518a3c599be389c to your computer and use it in GitHub Desktop.
Save emrahgunduz/2f7ae873175699d8c518a3c599be389c to your computer and use it in GitHub Desktop.
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