Plotting stacked bar charts

Stacked bar charts are of course possible by using a special parameter from the function.

How to do it...

The following script stacks two bar charts on each other:

import matplotlib.pyplot as plt

A = [5., 30., 45., 22.]
B = [5., 25., 50., 20.]

X = range(4), A, color = 'b'), B, color = 'r', bottom = A)

The preceding script will produce the following graph:

How to do it...

How it works...

The optional bottom parameter of the function allows you to specify a starting value for a bar. Instead of running from zero to a value, it will go from the bottom to value. The first call to ...

Get matplotlib Plotting Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.