Python is routinely used to produce a wide range of plots of data. Whilst spreadsheets may offer an alternative route for simple graphs, the use of programmatic methods is especially useful for repetitive analysis, as the same script can be applied sequentially to many files. Python also gives access to many types of plot which are beyond the capabilities of a spreadsheet application. The guides to regression may be useful to read in conjunction with this page.
This script reads in data from CSV files and outputs plotted graphs, and can be run on single, multiple or all CSV files within a directory. The script can be modified to produce many different plots, and is able to perform linear and non linear regression.
The script accepts filenames as arguments, eg:
python plot.py file1.csv
python plot.py file1.csv file2.csv
python plot.py *.csv
The script includes a number of configurable options which can be set in the script including:
Defining columns to be used for the x and y values
Axes labels
Limits for the axes
Data manipulation (see guide)
Inclusion of a line of best fit
Linear regression output (gradient and intercept values), including collation of the parameters into a separate file
Iterative non linear regression
The script is capable of carrying out calculations on the data prior to plotting. This might be a simple unit conversion, eg time in minutes to time in seconds, where x = x*60, or might involve more complex functions.The document below contains some example calculations. The calculations are using numpy. If you wish to perform an arithmetic function not listed in the support document, try searching 'numpy' and the function you wish to use.
Below are example scripts for various different plot types which may be useful. Before drawing a plot, it is important to consider what type of plot is best for the data and what you are trying to communicate.
A box plot is
script.py (python) ↓ (GitHub)
The sample data below can be used to show an example of this sort of plot. This data is...
A contour plot is
script.py (python) ↓ (GitHub)
The sample data below can be used to show an example of this sort of plot. This data is...