Get Started

Slice 3D images

The simplest way to plot 3D and 4D images by slicing them into many 2D frames. Plotting many slices sequentially can create a "fly-through" effect that helps you understand the image as a whole.

To select a 2D frame, pick a frame for the first axis and select all data from the remaining two: vol[0, :, :]

For this exercise, use for loop to plot every 40th slice of vol on a separate subplot. matplotlib.pyplot (as plt) has been imported for you.

This is a part of the course

“Biomedical Image Analysis in Python”

View Course

Exercise instructions

  • Using plt.subplots(), initialize a subplots grid with 1 row and 4 columns.
  • Plot every 40th slice of vol in grayscale. To get the appropriate index, multiply ii by 40.
  • Turn off the ticks, labels, and frame for each subplot.
  • Render the figure.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Plot the images on a subplots array 
fig, axes = ____

# Loop through subplots and draw image
for ii in range(4):
    im = ____
    axes[ii].imshow(____, cmap='gray')
    axes[ii].axis(____)
    
# Render the figure
plt.show()

This exercise is part of the course

Biomedical Image Analysis in Python

IntermediateSkill Level
4.6+
12 reviews

Learn the fundamentals of exploring, manipulating, and measuring biomedical image data.

Prepare to conquer the Nth dimension! To begin the course, you'll learn how to load, build and navigate N-dimensional images using a CT image of the human chest. You'll also leverage the useful ImageIO package and hone your NumPy and matplotlib skills.

Exercise 1: Image dataExercise 2: Load imagesExercise 3: MetadataExercise 4: Plot imagesExercise 5: N-dimensional imagesExercise 6: Stack imagesExercise 7: Load volumesExercise 8: Field of viewExercise 9: Advanced plottingExercise 10: Generate subplotsExercise 11: Slice 3D images
Exercise 12: Plot other views

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free