How to display a sequence of images using Matplotlib?

To display a sequence of images using Matplotlib, you can create an animated slideshow that cycles through multiple images. This technique is useful for comparing images, creating time-lapse visualizations, or building simple image presentations.

Basic Image Sequence Display

Here's how to display a sequence of images with automatic timing ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample images (since we can't load external files)
def create_sample_image(color, text):
    """Create a sample colored image with text"""
    img = np.ones((100, 150, 3))
    if color == 'red':
        img[:, :, 1:] = 0
    elif color == 'green':
        img[:, :, [0, 2]] = 0
    elif color == 'blue':
        img[:, :, :2] = 0
    return img

# List of sample images
images_data = [
    create_sample_image('red', 'Image 1'),
    create_sample_image('green', 'Image 2'),
    create_sample_image('blue', 'Image 3')
]

plt.axis('off')
img_plot = None

for i, im in enumerate(images_data):
    if img_plot is None:
        img_plot = plt.imshow(im)
        plt.title(f'Image {i+1}')
        plt.pause(1.0)  # Display for 1 second
    else:
        img_plot.set_data(im)
        plt.title(f'Image {i+1}')
        plt.pause(1.0)
    plt.draw()

plt.show()

Interactive Image Sequence

For a more controlled approach, you can create an interactive image viewer ?

import matplotlib.pyplot as plt
import numpy as np

def create_sample_images():
    """Create a set of sample images"""
    images = []
    colors = ['red', 'green', 'blue', 'yellow', 'purple']
    
    for i, color in enumerate(colors):
        img = np.random.rand(50, 80, 3)
        if color == 'red':
            img[:, :, [1, 2]] *= 0.3
        elif color == 'green':
            img[:, :, [0, 2]] *= 0.3
        elif color == 'blue':
            img[:, :, [0, 1]] *= 0.3
        elif color == 'yellow':
            img[:, :, 2] *= 0.3
        elif color == 'purple':
            img[:, :, 1] *= 0.3
            
        images.append(img)
    
    return images

# Create sample images
sample_images = create_sample_images()

# Display images in sequence
fig, ax = plt.subplots(figsize=(6, 4))
ax.set_title('Image Sequence Display')

for i, img in enumerate(sample_images):
    ax.clear()
    ax.imshow(img)
    ax.set_title(f'Image {i+1} of {len(sample_images)}')
    ax.axis('off')
    plt.draw()
    plt.pause(0.8)

plt.tight_layout()
plt.show()
Displays 5 colored images in sequence, each for 0.8 seconds

Using Animation for Smoother Transitions

For more professional animations, use Matplotlib's animation module ?

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

def create_gradient_images():
    """Create gradient images for animation"""
    images = []
    for i in range(5):
        # Create gradient effect
        x = np.linspace(0, 1, 100)
        y = np.linspace(0, 1, 80)
        X, Y = np.meshgrid(x, y)
        
        # Different gradient patterns
        if i == 0:
            img = np.stack([X, Y, np.ones_like(X) * 0.5], axis=2)
        elif i == 1:
            img = np.stack([Y, X, np.ones_like(X) * 0.8], axis=2)
        elif i == 2:
            img = np.stack([X*Y, np.ones_like(X) * 0.6, Y], axis=2)
        elif i == 3:
            img = np.stack([np.ones_like(X) * 0.7, X*Y, X], axis=2)
        else:
            img = np.stack([Y, X*Y, np.ones_like(X) * 0.9], axis=2)
            
        images.append(img)
    
    return images

# Create animation
fig, ax = plt.subplots()
gradient_images = create_gradient_images()

im = ax.imshow(gradient_images[0])
ax.set_title('Animated Image Sequence')
ax.axis('off')

def animate(frame):
    im.set_data(gradient_images[frame])
    ax.set_title(f'Frame {frame + 1}')
    return [im]

ani = animation.FuncAnimation(fig, animate, frames=len(gradient_images), 
                            interval=1000, blit=True, repeat=True)

plt.tight_layout()
plt.show()
Creates a smooth animation cycling through 5 gradient images

Key Points

  • plt.pause() creates delays between image displays

  • img.set_data() updates existing plot data efficiently

  • plt.draw() forces immediate redraw of the plot

  • Use matplotlib.animation for professional animations

  • Turn off axes with plt.axis('off') for cleaner display

Conclusion

Matplotlib provides multiple ways to display image sequences, from simple plt.pause() loops to sophisticated animations. Choose set_data() for efficient updates or FuncAnimation for smooth, professional presentations.

Updated on: 2026-03-26T00:30:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements