A Beginners Guide to Programming and Automated Analysis: Part 1 Graphs
Purpose
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.
Background
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.
Meat
Lets graph:
Lesson01.py
#plot stuff
Import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,1])
plt.show()
Break this down
Import
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.
Comments
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
as
The as keyword saves some typing by allowing us to alias the imported library to something shorter.
import matplotlib.pyplot as plt
plt.plot()
plt.plot()
plt.xlabel(“”)
plt.ylabel(“”)
plt.title(“”)
plt.grid(“”)
plt.show()
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