Automatic plotting with Python

Below is a sample Pyhton script to automatically plot a generic data file using matplotlib. It will automatically read an arbitrary number of datasets from a file and plot each as a separate line in a matplotlib plot.

Usage

python3 plot.py input_data_filename output_filename [plot_title [y_axis_label]]

The first argument is the name of the file containing the data (see below for formatting). The second argument is the name of the file to output. The third argument (optional) is the title of the plot, and the fourth argument (also optional) is the label to put on the y axis.

Input file formatting

Here is an example input file:

Position [m]    T (fluid)   T (solid)
0.000000e+00    2.780000e+02    2.905000e+02
1.666667e+00    2.790000e+02    2.880000e+02
3.333333e+00    2.795000e+02    2.870000e+02
5.000000e+00    2.790000e+02    2.820000e+02
6.666667e+00    2.780000e+02    2.899000e+02
8.333333e+00    2.800000e+02    2.880000e+02
1.000000e+01    2.805000e+02    2.780000e+02
1.166667e+01    2.780000e+02    2.780000e+02
1.333333e+01    2.770000e+02    2.790000e+02
1.500000e+01    2.720000e+02    2.795000e+02
1.666667e+01    2.799000e+02    2.790000e+02
1.833333e+01    2.780000e+02    2.780000e+02
2.000000e+01    2.680000e+02    2.800000e+02

The first line contains the labels. The first column specifies the values on the x axis, the second and third column each represent the y-axis values for a different dataset. The first label, “Position [m]” appears below the x axis, and the others appear in the legend.

The script

import sys
import matplotlib.pyplot as plt

if len(sys.argv) < 3:
    print("Error: too few arguments!")
    print("Usage: ./plot.py input_data_filename output_filename [plot_title [y_axis_label]]")
    exit()

# Read command line arguments
datafile = sys.argv[1]
outfile = sys.argv[2]

plottitle = ''
ylabel = ''
if len(sys.argv) >= 4:
    plottitle = sys.argv[3]
    if len(sys.argv) >= 5:
        ylabel = sys.argv[4]

# Read input data file
labels = []
data = []

with open(datafile) as f:
    content = f.read().splitlines()
    # First row is assumed to contain labels for each column
    labels = [l.strip() for l in content[0].split('\t')]

    # Use the number of labels to determine the number of data columns
    data = [[] for _ in range(len(labels))]
    for line in content[1:]:
        i = 0
        for num in line.split():
            data[i].append(float(num))
            i += 1

if ylabel == '' and len(labels) == 2:
    ylabel = labels[1]

# Initialize plot
fig, ax = plt.subplots()

# Plot each data set, using first column as x-axis data
for i in range(1, len(labels)):
    ax.plot(data[0], data[i], label=labels[i])

# Plot's aesthetics
ax.set_title(plottitle)
ax.set_xlabel(labels[0])
ax.set_ylabel(ylabel)
ax.legend()

# Save figure
fig.savefig(outfile)
plt.close(fig)

Example result

Running the command

python3 plot.py sample.dat plot.png "Temperature measurements" "Temperature [K]"

where sample.dat is the input file from the above example will yield the following image: