matplotlib.colors.Colormap class allows you to map scalar values to RGBA (Red, Green, Blue, Alpha) colors. This enhances the clarity and depth of your data representation, making it easier to interpret complex data patterns through effective color coding.
To illustrate the core concept, here's a simple example that creates a colormap and applies it to a scatter plot:
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
cmap = ListedColormap(['red', 'green', 'blue'])
plt.scatter(x, y, c=colors, cmap=cmap)
plt.colorbar()
plt.show()
Output:
simple colormap scatterplotA scatter plot where each point is color-mapped according to the values in colors
What is a Colormap in Matplotlib?
A colormap in Matplotlib is a method of mapping scalar values to colors. It helps in visualizing trends and patterns in data by applying a color gradient that represents data values. Colormaps are widely used in heatmaps, scatter plots, and other visualizations to make data interpretation more intuitive.
Core Features of Colormaps
- Scalability: Colormaps can be scaled to represent a wide range of data values.
- Customizability: You can create your own colormap or modify existing ones to suit your visualization needs.
- Variety: Matplotlib provides numerous built-in colormaps suitable for various types of data visualizations.
Using Colormaps in Matplotlib
Matplotlib offers many built-in colormaps, such as viridis
, plasma
, and copper
, which are stored in the matplotlib.colormaps
container. These colormaps can be accessed and applied easily to data visualizations.
Python
from matplotlib import colormaps
print(list(colormaps))
Output:
['magma', 'inferno', 'plasma', 'viridis', 'cividis', 'twilight', 'twilight_shifted', 'turbo', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'CMRmap', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PRGn', 'PiYG'] .. and Many more
Getting RGBA Values from a Colormap
To retrieve specific colors from a colormap, you can pass a scalar value between 0 and 1, which corresponds to a specific color in the colormap's range.
Python
import matplotlib
plasma = matplotlib.colormaps['plasma'].resampled(10)
print(plasma(0.65))
Output:
(0.928329, 0.472975, 0.326067, 1.0)
Creating Custom Colormaps
You can easily create your own colormap using either the ListedColormap or LinearSegmentedColormap classes.
Example: Custom ListedColormap
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
colors = ["blue", "green", "yellow"]
cmap = ListedColormap(colors)
data = np.random.randn(10, 10)
plt.imshow(data, cmap=cmap)
plt.colorbar()
plt.show()
Output :
ListedColormapExample: Custom LinearSegmentedColormap
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
colors = ["blue", "green", "yellow"]
cmap = LinearSegmentedColormap.from_list("CustomCmap", colors)
data = np.random.randn(10, 10)
plt.imshow(data, cmap=cmap)
plt.colorbar()
plt.show()
Output:
LinearSegmentedColormapCustomizing Colormaps
You can modify existing colormaps by changing properties like color limits or adding transparency using methods such as set_under, set_over, and set_bad.
Example: Modifying a Colormap
Python
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
data = np.random.rand(4, 4)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 4))
ax1.imshow(data)
ax1.set_title("Default colormap")
mpl.rc('image', cmap='plasma')
ax2.imshow(data)
ax2.set_title("Modified colormap")
plt.show()
Output:
changing the default colormap