Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to show two different colored colormaps in the same imshow Matplotlib?
To show two different colored colormaps in the same imshow matplotlib, we can use masked arrays to separate positive and negative values, then overlay them with different colormaps.
Understanding Masked Arrays
Matplotlib's masked_array allows us to hide certain data points. By masking positive values in one array and negative values in another, we can display them with different colormaps on the same plot.
Example
Let's create a visualization showing positive and negative values with different colormaps ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create a 2D matrix with random values between -10 and 10
img = np.random.randint(-10, 10, (5, 5))
print("Original data:")
print(img)
# Create masked arrays
data1 = np.ma.masked_array(img, img >= 0) # Mask positive values (show only negative)
data2 = np.ma.masked_array(img, img < 0) # Mask negative values (show only positive)
# Create the plot
fig, ax = plt.subplots()
# Display both masked arrays with different colormaps
img1 = ax.imshow(data1, cmap="prism_r")
img2 = ax.imshow(data2, cmap="copper")
# Create separate colorbars
bar1 = plt.colorbar(img1, ax=ax, shrink=0.8)
bar2 = plt.colorbar(img2, ax=ax, shrink=0.8)
# Set colorbar labels
bar1.set_label('Negative Values (prism_r)')
bar2.set_label('Positive Values (copper)')
plt.title('Two Different Colormaps in Same Plot')
plt.show()
The output displays the data with two different color schemes overlaid on the same image ?
Original data: [[ -3 8 -1 2 -9] [ 4 -7 6 -5 1] [ -2 9 -8 3 -6] [ 7 -4 5 -1 8] [ -9 2 -3 6 -7]]
How It Works
The technique uses these key concepts:
Masked Arrays:
np.ma.masked_array(data, condition)hides values where condition is TrueOverlay: Multiple
imshow()calls overlay the images, showing only unmasked valuesTransparency: Masked values are transparent, allowing the underlying data to show through
Alternative Approach with Custom Positioning
For better control over colorbar positioning ?
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Create sample data
img = np.random.randint(-10, 10, (6, 6))
# Create masked arrays
negative_data = np.ma.masked_array(img, img >= 0)
positive_data = np.ma.masked_array(img, img < 0)
fig, ax = plt.subplots(figsize=(8, 6))
# Display the images
im1 = ax.imshow(negative_data, cmap="Blues", alpha=0.8)
im2 = ax.imshow(positive_data, cmap="Reds", alpha=0.8)
# Create divider for proper colorbar positioning
divider = make_axes_locatable(ax)
cax1 = divider.append_axes("right", size="5%", pad=0.1)
cax2 = divider.append_axes("right", size="5%", pad=0.3)
# Add colorbars
cbar1 = plt.colorbar(im1, cax=cax1, label="Negative Values")
cbar2 = plt.colorbar(im2, cax=cax2, label="Positive Values")
ax.set_title('Controlled Colorbar Positioning')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Example |
|---|---|---|
cmap |
Colormap choice | "viridis", "plasma", "coolwarm" |
alpha |
Transparency level | 0.0 (transparent) to 1.0 (opaque) |
shrink |
Colorbar size | 0.8 makes colorbar 80% of plot height |
Conclusion
Use masked arrays with different colormaps to visualize distinct data ranges effectively. The np.ma.masked_array() function combined with multiple imshow() calls creates professional multi-colormap visualizations for complex datasets.
