Displaying multiple lines
This recipe will show you how to display multiple lines in a graph.
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:
>>> DATA = ( ... ('Q1 2017', 100, 5), ... ('Q2 2017', 105, 15), ... ('Q3 2017', 125, 40), ... ('Q4 2017', 115, 80), ... )
- Process the data to prepare the expected format:
>>> POS = list(range(len(DATA))) >>> VALUESA ...