How to apply pseudo color schemes to an image plot in Matplotlib?

Pseudocolor schemes can enhance contrast and make data visualization more effective, especially when presenting on projectors with poor contrast. Pseudocolor is particularly useful for single-channel grayscale images where different color maps can highlight various data patterns.

Pseudocolor is only relevant to single-channel, grayscale, luminosity images. Since R, G, and B channels are often similar in many images, we can extract one channel and apply different color schemes to visualize the data more effectively.

Basic Pseudocolor Application

Here's how to apply a pseudocolor scheme to an image using matplotlib ?

import matplotlib.pyplot as plt
import matplotlib.image as mimg
import numpy as np

plt.rcParams["figure.figsize"] = [12, 8]
plt.rcParams["figure.autolayout"] = True

# Create a sample grayscale image (since we can't load external files)
img_data = np.random.rand(100, 100) * 255
lum_img = img_data.astype(np.uint8)

# Apply different pseudocolor schemes
fig, axes = plt.subplots(2, 3, figsize=(12, 8))

# Original grayscale
axes[0, 0].imshow(lum_img, cmap='gray')
axes[0, 0].set_title('Original Grayscale')
axes[0, 0].axis('off')

# Hot colormap
axes[0, 1].imshow(lum_img, cmap='hot')
axes[0, 1].set_title('Hot Colormap')
axes[0, 1].axis('off')

# Jet colormap
axes[0, 2].imshow(lum_img, cmap='jet')
axes[0, 2].set_title('Jet Colormap')
axes[0, 2].axis('off')

# Viridis colormap
axes[1, 0].imshow(lum_img, cmap='viridis')
axes[1, 0].set_title('Viridis Colormap')
axes[1, 0].axis('off')

# Plasma colormap
axes[1, 1].imshow(lum_img, cmap='plasma')
axes[1, 1].set_title('Plasma Colormap')
axes[1, 1].axis('off')

# Cool colormap
axes[1, 2].imshow(lum_img, cmap='cool')
axes[1, 2].set_title('Cool Colormap')
axes[1, 2].axis('off')

plt.tight_layout()
plt.show()

Extracting Single Channel from RGB Image

When working with RGB images, you need to extract a single channel for pseudocolor application ?

import matplotlib.pyplot as plt
import numpy as np

# Create a sample RGB image
height, width = 50, 50
rgb_img = np.random.rand(height, width, 3) * 255
rgb_img = rgb_img.astype(np.uint8)

# Extract single channels
red_channel = rgb_img[:, :, 0]
green_channel = rgb_img[:, :, 1]
blue_channel = rgb_img[:, :, 2]

# Display channels with different colormaps
fig, axes = plt.subplots(1, 4, figsize=(12, 3))

axes[0].imshow(rgb_img)
axes[0].set_title('Original RGB')
axes[0].axis('off')

axes[1].imshow(red_channel, cmap='Reds')
axes[1].set_title('Red Channel')
axes[1].axis('off')

axes[2].imshow(green_channel, cmap='Greens')
axes[2].set_title('Green Channel')
axes[2].axis('off')

axes[3].imshow(blue_channel, cmap='Blues')
axes[3].set_title('Blue Channel')
axes[3].axis('off')

plt.tight_layout()
plt.show()

Popular Pseudocolor Schemes

Colormap Use Case Description
'hot' Heat maps Black to red to yellow to white
'jet' Scientific data Blue to cyan to yellow to red
'viridis' Perceptually uniform Dark purple to green to yellow
'plasma' High contrast Dark purple to pink to yellow

Custom Pseudocolor with Colorbar

Adding a colorbar helps interpret the pseudocolor mapping ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
data = np.random.rand(30, 30) * 100

# Create pseudocolor plot with colorbar
plt.figure(figsize=(8, 6))
im = plt.imshow(data, cmap='coolwarm')
plt.colorbar(im, label='Intensity Value')
plt.title('Pseudocolor Plot with Colorbar')
plt.axis('off')
plt.show()

Conclusion

Pseudocolor schemes enhance grayscale image visualization by mapping intensity values to colors. Choose colormaps based on your data type and audience − 'viridis' for scientific accuracy, 'hot' for heat maps, or 'jet' for high contrast presentations.

Updated on: 2026-03-25T22:12:26+05:30

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements