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 bar charts


The pyplot.scatter() function has a built-in support for colormaps; some other plotting functions that we will discover later also have such support. However, some functions, such as pyplot.bar(), do not take colormaps as inputs to plot bar charts. In this recipe, we are going to see how to color a bar chart with a colormap.

matplotlib has helper functions to explicitly generate colors from a colormap. For instance, we can color the bars of a bar chart with the functions of the values they represent.

How to do it...

We will use the matplotlib.cm module in this recipe just as we did in the previous recipe. This time, we will directly use a colormap object rather than letting a rendering function use it automatically. We will also need the matplotlib.colors module, which contains the utility functions related to colors as shown in the following script:

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

values...