How to create a matplotlib colormap that treats one value specially?

To create a matplotlib colormap that treats one value specially, we can use set_under(), set_over(), or set_bad() methods to assign special colors for out-of-range or invalid values.

Basic Approach Using set_under()

The set_under() method assigns a special color to values below the colormap range ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
data = np.random.randn(5, 5)
eps = np.spacing(0.0)

# Get colormap and set special color for low values
cmap = plt.get_cmap('rainbow')
cmap.set_under('red')

# Create plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(data, interpolation='nearest', vmin=eps, cmap=cmap)
fig.colorbar(im, extend='min')

plt.title('Colormap with Special Color for Low Values')
plt.show()

Using set_over() for High Values

Similarly, set_over() assigns a special color to values above the colormap range ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data with some high values
data = np.random.randn(5, 5)
data[0, 0] = 5.0  # Add a high value

# Get colormap and set special color for high values
cmap = plt.get_cmap('viridis')
cmap.set_over('orange')

# Create plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(data, interpolation='nearest', vmax=2.0, cmap=cmap)
fig.colorbar(im, extend='max')

plt.title('Colormap with Special Color for High Values')
plt.show()

Using set_bad() for Invalid Values

The set_bad() method handles NaN or masked values with a special color ?

import matplotlib.pyplot as plt
import numpy as np

# Create data with some NaN values
data = np.random.randn(5, 5)
data[2, 2] = np.nan  # Add a NaN value
data[1, 3] = np.nan  # Add another NaN value

# Get colormap and set special color for bad values
cmap = plt.get_cmap('coolwarm')
cmap.set_bad('black')

# Create plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(data, interpolation='nearest', cmap=cmap)
fig.colorbar(im)

plt.title('Colormap with Special Color for NaN Values')
plt.show()

Complete Example with Multiple Special Values

You can combine all three methods to handle different special cases ?

import matplotlib.pyplot as plt
import numpy as np

# Create complex data
data = np.random.randn(6, 6)
data[0, 0] = -5.0   # Very low value
data[1, 1] = 5.0    # Very high value
data[2, 2] = np.nan # Invalid value

# Setup colormap with multiple special colors
cmap = plt.get_cmap('plasma')
cmap.set_under('blue')    # Low values
cmap.set_over('red')      # High values
cmap.set_bad('white')     # NaN values

# Create plot with custom range
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(data, interpolation='nearest', 
               vmin=-2.0, vmax=2.0, cmap=cmap)
fig.colorbar(im, extend='both')

plt.title('Colormap with Multiple Special Values')
plt.show()

Key Parameters

  • vmin/vmax − Define the colormap range
  • extend − Controls colorbar extension ('min', 'max', 'both', or 'neither')
  • set_under/over/bad − Methods to set special colors
  • interpolation − Controls how pixels are displayed

Conclusion

Use set_under(), set_over(), and set_bad() to assign special colors for out-of-range or invalid values. Combine with vmin/vmax and extend parameters for complete control over colormap behavior.

Updated on: 2026-03-25T23:53:45+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements