Matplotlib Colormaps
Matplotlib is a popular data visualization library in Python that provides a wide range of functionalities for creating high-quality plots and charts. One important aspect of creating visually appealing plots is choosing the right colormap.
In this article, we will explore different colormaps provided by Matplotlib, understand how colormaps work, how to use them in your plots, and how to customize them to suit your specific needs.
What is a Colormap?
A colormap, also known as a color map or a color scale, is a mapping of scalar values to colors. It is used to represent quantitative data in an informative and visually appealing way. Matplotlib provides a variety of built-in colormaps that you can use to customize the colors of your plots.
Available Colormaps in Matplotlib
Matplotlib provides a wide range of built-in colormaps that you can choose from based on your data and visualization requirements. Some of the commonly used colormaps in Matplotlib include:
- viridis
- plasma
- inferno
- magma
- cividis
- coolwarm
- bone
- spring
- autumn
- winter
Let’s dive into some examples of using these colormaps in Matplotlib plots.
Example 1: Using the viridis
Colormap
The viridis
colormap is a perceptually uniform colormap designed for use in visualization tasks. Let’s see how to use the viridis
colormap in a simple scatter plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.show()
Output:
In this example, we generate random data points and assign random colors to each point using the viridis
colormap. The resulting scatter plot will have a smooth color gradient that is visually appealing.
Example 2: Using the plasma
Colormap
The plasma
colormap is similar to the viridis
colormap but has a different color scheme. Let’s see how to use the plasma
colormap in a contour plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
plt.contourf(X, Y, Z, cmap='plasma')
plt.colorbar()
plt.show()
Output:
In this example, we create a contour plot of a 2D sinusoidal function using the plasma
colormap. The resulting plot will have a vibrant color scheme that enhances the visualization of the data.
Example 3: Using the inferno
Colormap
The inferno
colormap is a dark color scheme that is useful for highlighting details in plots. Let’s see how to use the inferno
colormap in a surface plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='inferno')
fig.colorbar(surf)
plt.show()
Output:
In this example, we create a 3D surface plot of a radial sinusoidal function using the inferno
colormap. The dark color scheme of the colormap enhances the visual contrast in the plot.
Example 4: Using the coolwarm
Colormap
The coolwarm
colormap is a diverging colormap that transitions from cool colors to warm colors. Let’s see how to use the coolwarm
colormap in a heatmap plot.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(10, 10)
plt.imshow(data, cmap='coolwarm')
plt.colorbar()
plt.show()
Output:
In this example, we create a heatmap plot of random data using the coolwarm
colormap. The resulting plot will have a smooth transition from cool colors (blue) to warm colors (red).
Example 5: Using the bone
Colormap
The bone
colormap is a grayscale colormap that is useful for highlighting edges and contours in plots. Let’s see how to use the bone
colormap in an image plot.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(100, 100)
plt.imshow(data, cmap='bone')
plt.colorbar()
plt.show()
Output:
In this example, we create an image plot of random data using the bone
colormap. The grayscale color scheme enhances the visual representation of the data.
Example 6: Using the spring
Colormap
The spring
colormap is a colormap with a bright, warm color scheme. Let’s see how to use the spring
colormap in a bar plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
y = np.random.rand(10)
plt.bar(x, y, color=plt.cm.spring(y/max(y)))
plt.show()
Output:
In this example, we create a bar plot with colors mapped to the spring
colormap based on the value of the data points. The resulting plot will have a vibrant color scheme that enhances the visual appeal.
Example 7: Using the autumn
Colormap
The autumn
colormap is a colormap with warm colors that transition from yellow to red. Let’s see how to use the autumn
colormap in a line plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, color=plt.cm.autumn(x/10))
plt.show()
In this example, we create a line plot with colors mapped to the autumn
colormap based on the value of the x-axis. The resulting plot will have a warm color scheme that enhances the visualization of the data.
Example 8: Using the winter
Colormap
The winter
colormap is a colormap with cool colors that transition from blue to white. Let’s see how to use the winter
colormap in a scatter plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
plt.scatter(x, y, c=colors, cmap='winter')
plt.colorbar()
plt.show()
Output:
In this example, we generate random data points and assign random colors to each point using the winter
colormap. The resulting scatter plot will have a cool color scheme that is visually appealing.
Customizing Colormaps
In addition to using the built-in colormaps provided by Matplotlib, you can also customize colormaps to suit your specific needs. One way to customize colormaps is by creating your own colormap using the ListedColormap
class.
Example 9: Customizing a Colormap
Let’s create a custom colormap that transitions from blue to red using the ListedColormap
class.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
colors = [(0, 0, 1), (1, 0, 0)] # Blue to red colormap
cmap_custom = ListedColormap(colors)
x = np.linspace(0, 1, 100)
y = x**2
plt.scatter(x, y, c=y, cmap=cmap_custom)
plt.colorbar()
plt.show()
Output:
In this example, we create a custom colormap that transitions from blue to red using the ListedColormap
class. We then apply this custom colormap to a scatter plot of quadratic data points.
Example 10: Reversing a Colormap
You can also reverse a colormap to change the direction of the color gradient. Let’s see how to reverse the coolwarm
colormap.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(10, 10)
plt.imshow(data, cmap='coolwarm_r') # `_r` suffix reverses the colormap
plt.colorbar()
plt.show()
Output:
In this example, we create a heatmap plot of random data using the reversed coolwarm
colormap. The resulting plot will have a reversed color scheme that transitions from warm colors to cool colors.
Example 11: Adding Transparency to a Colormap
You can add transparency to a colormap by adjusting the alpha (transparency) values of the colors. Let’s see how to add transparency to the viridis
colormap.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
rgba_colors = plt.cm.viridis(colors)
rgba_colors[:, 3] = 0.5 # Set alpha value to 0.5 for transparency
plt.scatter(x, y, c=rgba_colors)
plt.show()
Output:
In this example, we generate random data points and assign random colors to each point using the viridis
colormap with added transparency. The resulting scatter plot will have transparent data points with a smooth color gradient.
Example 12: Discretizing a Colormap
You can discretize a colormap by converting it into a ListedColormap
with a specified number of colors. Let’s see how to discretize the viridis
colormap into 4 colors.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
cmap_viridis = plt.cm.get_cmap('viridis', 4)
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
plt.scatter(x, y, c=colors, cmap=cmap_viridis)
plt.colorbar()
plt.show()
In this example, we generate random data points and assign random colors to each point using a discretized version of the viridis
colormap with 4 colors. The resulting scatter plot will have a discrete color scheme.
Matplotlib Colormaps Conclusion
In this article, we explored different colormaps provided by Matplotlib and learned how to use them in various types of plots. We also saw how to customize colormaps to suit our specific needs by creating custom colormaps, reversing colormaps, adding transparency, and discretizing colormaps.
Colormaps play a crucial role in data visualization by enhancing the visual representation of quantitative data. By choosing the right colormap and customizing it, you can create visually appealing and informative plots that effectively communicate your data insights.