A Beginners Guide to Programming and Automated Analysis: Part 1 Graphs


This tutorial series is intended to teach basic programming from the ground up in a way that will allow the you to learn the foundational principles but also quickly use it in your day-to-day work.

In my years as an engineer I deal with a lot of data. Typically most setups use excel and minitab to process this data. Excel is a powerful tool but is not driven for the presentation of various kinds of data. This is especially true for engineering data.

Excel is limited in the formats it can accept and the types of graphs it can output. The graphs it can output are not always easy to customize. Excel can be customized through Macros and other features but this is not trivial for complex tasks and graphs and data formats. Excel is nice if I want to produce a graph like this:

simple graph

what if I want to produce 20 of these or 2000? What if I want to produce a graph like this?:

You can use macros to extend the functionality but now your code and data are linked. Macros have long been a security risk. Macros are also limited in their extensibility.

There are alternatives to excel such as Minitab and crystal reports but they have similar limitations. Matlab will allow you to do much of these things but like many of the other tools also has a license fee that may be prohibitive for your situation.

Python is a programming language similar to Matlab, java, javascript, and c++. It is free so no worries about licenses and open source with a very active community which means that features and tutorials are very easy to find. It is designed to be easy to read and write while being powerful.

The python standard can be found at https://www.python.org/ with documentation at https://www.python.org/doc/

Due to the open source nature there are numerous flavors of pythons suited for different purposes. The distribution this tutorial series will use is Enthought Canopy found at epd.org. Other python distributions can be used but this series requires the matplotlib and required components. The version used at the time of this writing is version qwer.

Python code can be written in any text editor that can save simple txt files. Just be sure to save it with the extension .py. The easiest way to execute is to run canopy in your environment which is installed with enthought. If you are comfortable with the command line you can run your code form the terminal with the command python code.py where code.py is the name of your code.

Lets graph:

#plot stuff
Import matplotlib.pyplot as plt


Break this down

Python includes core capabilities but is also extendable. These extensions are organized into libraries accessible through the import keyword.

Plot(), show(), ()
These are functions. These functions belong to the pyplot class. Functions are similar to the concept of functions in algebra; they have names, inputs, and outputs. The plot function expects two lists representing x and y coordinates. The list sizes must match. The show function takes no arguments. You may be wondering why do you need a plot and show function? plot draws to a figure in the background. show displays this figure onto your screen.

Python has two mechanisms for writing non exeutable code. These are called comments and are helpful for documenting the coding process. This can be especially helpful for large scripts that may be changed long after they are written.

This graph is a little bland so lets spice it up
The as keyword saves some typing by allowing us to alias the imported library to something shorter.

import matplotlib.pyplot as plt


Now you know how to make simple graphs. Pyplot can make several types more and they are all documented at. Feel free to play araound

Leave a Reply