Drawing stacked bars
A powerful way of displaying different categories is to present them as stacked bars so that each of the categories and the total are displayed. We'll see how to do that in this recipe.
Getting ready
We need to install matplotlib
in our virtual environment:
$ echo "matplotlib==3.2.1" >> requirements.txt
$ pip install -r requirements.txt
If you are using macOS, you may get an error like this: RuntimeError: Python is not installed as a framework. See the matplotlib
documentation on how to fix it: https://matplotlib.org/faq/osx_framework.html.
How to do it...
- Import
matplotlib
:>>> import matplotlib.pyplot as plt
- Prepare the data. This represents two products' sales; an established one and a new one:
>>> DATA = ( ... ('Q1 2017', 100, 0), ... ('Q2 2017', 105, 15), ... ('Q3 2017', 125, 40), ... ('Q4 2017&apos...