Python is routinely used to produce plots of data. While spreadsheets offer an alternative route for simple graphs, the using Python is especially useful for repetitive analysis, as the same script can be applied to many files. Python also gives access to many types of plot which are beyond the capabilities of a spreadsheet application; see the Advanced plots page for some examples. The guides to regression may be useful to read in conjunction with this page.
plot.py (python) ↓ (GitHub) - Right click and Save Link As... to download.
This script reads in data from CSV files and outputs 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)
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.
The plot.py script can also be modified to add a linear or non-linear regression line to the plot. This is not enabled by default, but is easy to include by modifying the variable fit to be equal to True at the start of the script. This will add a linear line of best fit to your data. In addition, if the variable output is set to True a new csv file will be created containing the linear fit parameters for every data file processed by the script.
Alternatively, you can add an additional argument when you run the script to fit a custom function to the data. The functions "linear" and "quadratic" are already defined in the code; any user defined function (e.g. exponential, trigonometric) can be added to the script and then added as an argument when running the script. Note that the name of the function you wish to fit must be the final argument:
python plot.py file1.csv linear
python plot.py file1.csv file2.csv quadratic
python plot.py *.csv my_special_function
When adding a regression line in this way, the fit parameters are always output.