Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using colormaps for scatter plots


When using a lot of colors, defining each color one by one is tedious. Moreover, building a good set of colors is a problem in itself. In some cases, colormaps can address those issues. Colormaps define colors with a continuous function of one variable to one value, corresponding to one color. matplotlib provides several common colormaps; most of them are continuous color ramps. In this recipe, we are going to see how to color scatter plots with a colormap.

How to do it...

Colormaps are defined in the matplotib.cm module. This module provides functions to create and use colormaps. It also provides an exhaustive choice of predefined color maps.

The function pyplot.scatter() accepts a list of values for the color parameter. When providing a colormap (with the cmap parameter), those values will be interpreted as a colormap index as follows:

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

N = 256
angle  = np.linspace(0, 8 * 2 * np...