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
Selected Reading
How to view all colormaps available in Matplotlib?
Matplotlib provides numerous built-in colormaps for visualizing data. You can view all available colormaps programmatically or create animations to cycle through them.
Listing All Available Colormaps
The simplest way to see all colormap names is using plt.colormaps() ?
import matplotlib.pyplot as plt
# Get all colormap names
colormaps = plt.colormaps()
print(f"Total colormaps available: {len(colormaps)}")
print("First 10 colormaps:", colormaps[:10])
Total colormaps available: 166 First 10 colormaps: ['Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r']
Displaying Colormap Categories
Colormaps are organized into categories like sequential, diverging, and qualitative ?
import matplotlib.pyplot as plt
# Sequential colormaps
sequential = ['viridis', 'plasma', 'inferno', 'magma', 'Blues', 'Greens']
print("Sequential colormaps:", sequential)
# Diverging colormaps
diverging = ['coolwarm', 'bwr', 'seismic', 'RdBu', 'RdYlBu']
print("Diverging colormaps:", diverging)
# Qualitative colormaps
qualitative = ['Set1', 'Set2', 'tab10', 'Accent', 'Pastel1']
print("Qualitative colormaps:", qualitative)
Sequential colormaps: ['viridis', 'plasma', 'inferno', 'magma', 'Blues', 'Greens'] Diverging colormaps: ['coolwarm', 'bwr', 'seismic', 'RdBu', 'RdYlBu'] Qualitative colormaps: ['Set1', 'Set2', 'tab10', 'Accent', 'Pastel1']
Animating Through All Colormaps
Create an animation that cycles through all available colormaps to visualize them ?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')
# Create sample data
data = np.random.rand(5, 5)
im = ax.imshow(data)
cb = fig.colorbar(im, cax=cax)
# Get all colormaps
colormaps = plt.colormaps()
tx = ax.set_title('Colormap: {0}'.format(colormaps[0]))
def animate(i):
cax.cla()
data = np.random.rand(5, 5)
im = ax.imshow(data, cmap=colormaps[i])
fig.colorbar(im, cax=cax)
tx.set_text('Colormap: {0}'.format(colormaps[i]))
ani = animation.FuncAnimation(fig, animate, frames=len(colormaps), interval=200)
plt.show()
Simple Static Display
Display a sample image with different colormaps side by side ?
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = np.random.rand(10, 10)
# Display with different colormaps
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
cmaps = ['viridis', 'plasma', 'coolwarm']
for i, cmap in enumerate(cmaps):
im = axes[i].imshow(data, cmap=cmap)
axes[i].set_title(f'Colormap: {cmap}')
plt.colorbar(im, ax=axes[i])
plt.tight_layout()
plt.show()
Conclusion
Use plt.colormaps() to list all available colormaps in Matplotlib. Create animations or side-by-side comparisons to visualize different colormap effects on your data.
Advertisements
