0
0
Matplotlibdata~5 mins

Colormaps (sequential, diverging, qualitative) in Matplotlib

Choose your learning style9 modes available
Introduction

Colormaps help us show data with colors. They make patterns easy to see.

When showing data that goes from low to high, like temperature changes.
When comparing two extremes, like profit and loss.
When showing categories, like types of fruits or animals.
When making charts easier to understand with colors.
When you want to highlight differences in data clearly.
Syntax
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Use a colormap in a plot
plt.imshow(data, cmap='colormap_name')
plt.colorbar()
plt.show()

Replace 'colormap_name' with the name of the colormap you want.

Colormaps are grouped as sequential, diverging, or qualitative.

Examples
Uses a sequential colormap good for data that goes from low to high.
Matplotlib
plt.imshow(data, cmap='viridis')
Uses a diverging colormap to show two extremes with a middle point.
Matplotlib
plt.imshow(data, cmap='coolwarm')
Uses a qualitative colormap for different categories or groups.
Matplotlib
plt.imshow(data, cmap='tab10')
Sample Program

This code shows three plots side by side. Each uses a different colormap type to help see data in different ways.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Create example data
np.random.seed(0)
data = np.random.rand(10, 10)

# Plot with sequential colormap
plt.subplot(1, 3, 1)
plt.title('Sequential (viridis)')
plt.imshow(data, cmap='viridis')
plt.colorbar()

# Plot with diverging colormap
plt.subplot(1, 3, 2)
plt.title('Diverging (coolwarm)')
plt.imshow(data - 0.5, cmap='coolwarm')  # Center around zero
plt.colorbar()

# Plot with qualitative colormap
categories = np.random.randint(0, 10, (10, 10))
plt.subplot(1, 3, 3)
plt.title('Qualitative (tab10)')
plt.imshow(categories, cmap='tab10')
plt.colorbar(ticks=range(10))

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Sequential colormaps are good for ordered data that goes from low to high.

Diverging colormaps highlight differences around a middle value, like zero.

Qualitative colormaps are best for categories without order.

Summary

Colormaps use colors to show data patterns clearly.

Use sequential for data that changes from low to high.

Use diverging to show two opposite extremes.

Use qualitative for different groups or categories.