Plot your data in Python
Learn how to programmatically draw charts using the use the matplotlib library with Python. Darren Yates explains how.
Python is one of the world’s most popular programming languages — and it’s not too hard to see why. It’s free, easy to learn and incredibly versatile. Already, we’ve looked at how you can use Python to read data from your car’s engine control unit (ECU) and how Python can tap into features such as Speech Recognition on your Android device. The language is also equally adept at online activities, for example, Django ( djangoproject.com) is a popular web framework for developing complex websites using Python code. But Python is also popular in scientific, mathematical and statistical circles, thanks to its highly-advanced ability at drawing charts and plots via the matplotlib library and the SciPy stack.
GET PYTHON
Not sure if you have Python installed on your computer? That’s easily fixed — launch a command prompt on your Windows PC or terminal on your macOS or Linux system, type python and press Return. If all you get is an error message, it’s not installed. In that case, head to python.org/downloads and grab the latest version of Python 3 (we use Python 3 here at Masterclass Central). Launch the installer to install it onto your system.
PYPI – APP STORE FOR CODERS
When you run Python for the first time, it only comes with one library called the Python Standard Library — still, that one library is chock-full of functions and extensions, covering everything from the basics of print() and input(), through to multimedia, database, web, file input/output (I/O) and beyond.
However, it doesn’t do everything and for the things it can’t do, software developers create their own libraries, which is why the PyPI — Python Package Index — exists. Think of it as the App Store for Python programmers. At last count, it contained over 116,000 separate downloadable and code-able Python packages that you can import and include in your code. We’ve already used the Python-OBD library from here to read car engine computer data. You can browse through the index online by heading to pypi.python.org/pypi and tapping ‘Browse packages’ from the left-side menu.
INSTALL MATPLOTLIB LIBRARY
Creating charts and plots might not seem the most exciting thing you can do with Python, but matplotlib can do some seriously cool tricks we can’t show on paper — tricks like chart animations that are impressive to watch. To install matplotlib, we need ‘pip’ — the Python package installer that comes bundled with the standard Python download. You can quickly check to see if pip is part of you system’s environment path by launching a command prompt, typing ‘pip’ and pressing enter. If you get details of pip’s command switches, you’re good to go. If not, Windows users should find pip located in the subfolder
path /users/<user>/AppData/Local/ Programs/Python/<Python version>/ Scripts. You install matplotlib from the command prompt using the command: pip install matplotlib
Within a few seconds, the installation should be complete and you’ll be able to start importing the library into your Python code.
THE SCIPY STACK
However, if you’re going to install matplotlib, the chances are pretty high you’re also going to need NumPy, a library package that, among other things, provides fast multidimensional array support and is used in everything from engineering to data mining. These libraries also generally work together with SciPy, which adds in lots of useful mathematical extras such as Fast Fourier Transforms and linear algebra.
If you’re running macOS or Linux, you can install these libraries easily through pip and PyPI. Unfortunately for Windows users, the PyPI version of SciPy doesn’t install correctly using pip, but there is an alternative. Python packages are known also as ‘wheels’, from their ‘.whl’ file extension. Christoph Gohlke, from the Laboratory of Fluorescence Dynamics at the University of California, Irvine (UCI) provides a large (unofficial) collection of Windows-ready .whl packages and one of these is SciPy. You’ll notice a number of different versions of SciPy available, depending on the versions of Python (2.7, 3.4, 3.5 or 3.6) and the Windows build (32-bit, 64-bit) you’re running. For example, if you’re running the standard 32-bit Windows version of Python 3.6, you’d want ‘scipy-0.19.1cp36-cp36m-win32.whl’. Whatever your option, head to lfd.uci. edu/~gohlke/pythonlibs/#scipy and download the appropriate file.
You’re also going to need the UCI version of NumPy called ‘numpy+mkl’ — MKL is the Maths Kernel Library created by Intel (yep, that Intel), so make sure you download the appropriate version of that file as well from lfd.uci.edu/~gohlke/pythonlibs. Once downloaded, open up a command prompt at the file location and type the command:
pip install numpy1.13.1+mkl-cp36-cp36m-win32. whl And follow it up with: pip install scipy-0.19.1cp36-cp36m-win32.whl
You must install them in this order, otherwise SciPy won’t install correctly. That aside, once the installs are done, you’ll now have ready-to-use what’s often termed the ‘SciPy stack’, the collection of Python libraries used most often in maths, science and engineering fields. To check they’re good to go, launch the Python IDLE environment and type:
import scipy import numPy import matplotlib
Pressing Enter on each one. If you don’t see any error messages, you’re done.
RUNNING EXAMPLE CODE
Now we’re ready to run some code and rather than having to start from scratch, matplotlib has some excellent and impressive examples for you to play with. Head over to matplotlib.org/ examples/index.html and start by downloading the ‘simple_anim’ source code from the ‘animation Examples’ section. Open it in Python IDLE and run it (press F5). Within a second or so, you should start seeing a scrolling sine wave display in a separate window, a bit like an oscilloscope screen. Animations can be really useful for plotting data because they can help identify patterns not easily visible in fixed single-frame chart.
Another really cool animation is the ‘double_ pendulum_animated’ example — this one needs the full SciPy stack, so it’s also a good test to ensure you have everything installed correctly, otherwise it won’t work. It might look quite basic — a double pendulum swinging under the influence of gravity — but the fact it’s all modelled in mathematical formulas is really cool. Not only that, you can also tweak the settings to see how a change of gravity constant, pendulum mass and length change the swing pattern.
One more simple example that’s really clever is ‘wire3d_demo’. It’s only half-a-dozen lines of code to produce a three-dimensional wire plot of some data, but if you click-and-drag the plot area, you can spin it around in 3D space and see the 3D surface of the data. Well, even if it’s not particularly your cup of tea, it’s still damn impressive, if for no other reason that just how little code is required.
MAKING YOUR OWN CHARTS
Nevertheless, while playing around with examples is an ideal way to start (as you can see how various commands are used in practice), there’s nothing like having a go at writing your own code. We’ll get you started with a ‘hello world’ simple chart, just to break the ice. Grab this month’s source code pack from the APC website ( apcmag.com/ magstuff) and load the ‘hellochart.py’ source file. The basic 2D chart function inside matplotlib is called ‘pyplot’, so we start by importing this into our code as the object ‘plt’. We then create two lists or arrays called ‘pltX’ and ‘pltY’. The for-loop creates the data, adding the numbers 0 to 10 to pltX and the square of those numbers to pltY. The hard work is done by the next line:
plt.plot(pltX, pltY)
This creates a two-dimensional (2D) line plot with pltX on the horizontal or X-axis and pltY on the vertical or Y-axis. We then use the title() function to add a title, along with the xlabel() and ylabel() functions to add axis labels. Finally, we display the chart using the show() function.
The key thing to notice is how little we had to do — the pyplot module does all the drawing, ensures the Y-axis title is rotated, even creates the window and selects the line-plot colour. You can edit all these parameters if you wish, but it’s great to know you can throw together a quick plot with just a few lines of code.
HOW ANIMATIONS WORK
The easiest way to understand how plot animations work in matplotlib is to take a look at the code in an example file. The ‘simple_anim.py’ source code we looked before is an ideal place to start.
Animations are part of the animation module within the matplotlib library. The ‘simple_anim’ Python code begins with importing the three main modules required — the NumPy library plus the pyplot and animation modules. Next, the code
creates two matplotlib.figure objects called ‘fig’ and ‘ax’ — these are known as ‘tuples’ (kind of like a list or an array). They’re created using the subplot() function and contain the figure or plot we want to draw. After that, the ‘np.arange’ statement sets up a single-dimension array ranging from 0 to ‘2 x pi’ (6.28…) stepped at 0.01 intervals. The line following creates the plot of the sine wave, with ‘x’ on the X-axis and ‘np.sin(x)’ on the Y-axis.
Now we come to two Python functions — animate() and init(). The first is responsible for stepping through the animation while init() begins the animation cleanly.
Finally, there’s the ‘money’ statement — the animation code itself. As you can see, it’s just one line:
ani = animation. FuncAnimation(fig, animate, np.arange(1, 200), init_ func=init, interval=25, blit=True)
Matplotlib has two animation methods — FuncAnimation and ArtistAnimation. The main difference between them is FuncAnimation derives each frame change from a function or formula, while ArtistAnimation is a series of pre-fab frames ready to draw.
FuncAnimation has a number of parameters that determine the what, how and when of the animation. The most common way to animate computer graphics is to create a code device called a ‘timer’, which fires off a trigger after a pre-defined interval, launching a particular code function that updates the animation with a new frame (we’ve used something similar in this month’s Arduino Masterclass and our scrolling sound level meter project). Matplotlib uses this same technique. The parameters list (inside the braces) begins with the ‘fig’ plot object we want drawn, the second parameter is the ‘animate’ function to be called each time the timer triggers. Next, the np.arange() function creates an array of integers from 0 to 200. FuncAnimation allows you to set an initiatising function (init_func), called the first time the FuncAnimation() code runs. The ‘interval’ variable contains the timer delay between frames (25-milliseconds), while ‘bliting’ is a technique for ensuring the animation is smooth. Last of all, the plt.show() statement displays the animation.
It’ll probably seem quite complicated the first time you look at it, but when you consider how much is being hidden by those few commands, it’s no wonder matplotlib is popular.
At this point, the thing to do that I can’t recommend enough is to play around with the myriad of code examples — change parameters, edit code, even break the code to see how it all works. Keep a copy of the matplotlib API documentation nearby ( matplotlib.org/api/index.html) so that you can understand why functions perform the way they do and what each of the parameters does.
LOTS MORE FEATURES
We’ve barely scratched the surface of what matplotlib’s animation functions can do, let alone the rest of matplotlib library itself — for instance, the animation function can even tap into the Ffmpeg video transcoder if you install it onto your PC and turn animations into video files using whichever video codec you like from its libavcodec library. Combine NumPy, matplotlib and SciPy together and it’s no surprise Python is so popular within the mathematics, science and engineering communities.
SIMPLER INSTALLATION OPTIONS
The basic IDLE editor included with the standard Python install is serviceable, but as you start getting into more complex tasks requiring multiple libraries, like the SciPy stack, there are potentially easier ways to get moving. There are plenty of alternative Pythonready or Python-specific IDEs with a more advanced flavour and we’ll look at some of them in a future issue, but two worth looking at are WinPython ( winpython.github.io) and Anaconda ( www.anaconda.com). Both incorporate Spyder, an IDE designed for the scientific community, with the SciPy stack installed ready to go.
CODE IT YOURSELF
The great thing about programming is the power and freedom it gives you to roll out your own apps to fill gaps and solve problems, whether it’s at home or at work. But it’s also the ability to mod your code as required without needing someone else doing it for you. If you’ve never learned to program before, now is a great time and Python is arguably one of the simplest and most versatile languages around. Why not start now? You won’t regret it.