Using gnuplot from Python

According to my revision control systems (rcs in those days), I’ve been
using gnuplot to make graphs since at least 2002. And I’ve got it set up via
a custom gnuplotrc to match the style of the TeX documents I often use the
graphs in.

At work we have an Instron 3369 machine for material testing. Recently,
I wanted to visualize some tensile test data in ways that I couldn’t get into
the test report.

The “Bluehill” software that controls the Instron machine outputs results in
different formats. Natively, it produces a binary blob and UTF-16 encoded
XML. With some open-source (xmllint and unidecode) I could read that as
shown below.

xmllint --format getalit-05.is_tens | unidecode -e UTF-16 | less

This is somewhat readable and contains all the settings and results as far as
I can tell. However, I consider XML a pain in the ass to work with and will
use it only when no other alternatives exist.

Fortunately, I had also instructed the software to save the data in csv
format. The summary was saved in a file with the extension
<testname>.is_tens_Results.csv, while the raw data was saved in files in
a subdirectory <testname>.is_tens_RawData/Specimen_RawData_<N>.csv, where
<testname> is the name you have given the test, and <N> is the
specimen number.

The contents of the is_tens_Results.csv file contain whatever data you
instructed the program to save. In this case, I saved (among other things) the
Young’s modulus and the breaking stress of tensile test samples. I didn’t
explicitly save the breaking strain, so I calculated that from the modulus and
breaking stress.

Data from six different tests has to be gathered and plot in a single graph.
To facilitate this, I made tuples of file numbers and labels describing the
samples. But first the subprocess module is imported to use later.

First the data is read into lines. The lines of data that matter have the
following format (presumable because this software was configured for a Dutch
locale where the comma is used as the decimal separator):

2;"8.741";"76,11";"896";"100";"0,58";"20,3";"11,77"

They start with a number, then contain values in double quotes separated by
semicolons and using a decimal comma and a point as the thousands separator.
The first number is the specimen number. The second number is the Young’s
modulus and the third number is the tensile stress.

After reading all the lines in the file, we restrict the data to those lines
that start with a number. This format is still not suitable for parsing,
so I use a custom translation (the tr dictionary shown earlier).

In short, this translation removes the quotes and the full stops used as
thousands separators and replaces the decimal comma’s with decimal points.
After the translation the string is split on the semicolons. The Young’s
modulus (E) and breaking stress (σ) are extracted from the data. After that,
the breaking strain (ε) is calculated. This takes advantage of the ability of
Python 3 to use Unicode identifiers. The strain and stress are formatted as
a string which is finished with a line containing just an e and then
appended to the specimendata list. Once a file has been successfully
processed, the specimen data are added to the total data list. A line for
plotting this data is appended to the gpplots list.

Once all the data has been processed, the comma at the end of the last
gpplots string is removed, and all the commands are join-ed with
spaces. The resulting string is appended to the gpcmds list. The last
addition to gpcmds is the total gpdata list. Finally, gpcmds is
converted into a string by joining the lines with a newline.

# Write gnuplot commands to a file for debuggingwithopen('overzicht-trektesten.gp','w')asgpfile:gpfile.write(gpcmds)# Run gnuplotp=subprocess.run(['gnuplot'],input=gpcmds.encode('utf-8'))ifp.returncode!=0:print('gnuplot failed with error code',p.returncode)

To aid in debugging, the complete list of gnuplot commands is written to a file.

Finally, the gnuplot commands are encoded as UTF-8 and provided to the
standard input of a gnuplot process, which produces a PDF file. An image
of the graph in shown below.

Note that the styles are not gnuplot‘s defaults. The line styles are from
set1.pal from the gnuplot-palletes repository.