Reputation: 1893
I'm trying to print four samples of DICOM images to my notebook.
I've created the following:
import os
import pydicom as dicom
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
PATH = '../data/train/'
files = os.listdir(PATH)
np.random.seed(42)
random_four = np.random.randint(0, len(files), size=4)
subplots = [[0,0],[0,1],[1,0],[1,1]]
fig, axs = plt.subplots(2,2, figsize=(8,8))
for image_number in range(4):
sample = dicom.dcmread(PATH+files[random_four[image_number]])
axs[subplots[image_number]] = plt.imshow(sample.pixel_array, cmap='gray')
This produces a subplot, but only the last image actually shows:
How do I correctly print all four of these images to a notebook using subplots?
Upvotes: 0
Views: 521
Reputation: 50143
Use Axes.imshow
.
Also you can use zip
and axs.ravel()
to loop over the subplots:
fig, axs = plt.subplots(2, 2, figsize=(8,8))
for ax, image_number in zip(axs.ravel(), range(4)):
sample = dicom.dcmread(PATH+files[random_four[image_number]])
ax.imshow(sample.pixel_array, cmap='gray')
Or using enumerate
:
for image_number, ax in enumerate(axs.ravel()):
Upvotes: 2