""" Exercise 5.18 from " A primer on..." Read data from file and plot it. The exercise does not ask us to write a function, but we do it anyway, since this makes it easier to reuse the code in exercise 5.18. The call to the function is placed in a so-called "test block" at the bottom, which ensures that this code is executed when the file is run as a stand-alone program, but not if the file is imported as a module into another program. """ import sys import numpy as np import matplotlib.pyplot as plt def read_density_data(filename): temp = [] density = [] with open(filename) as infile: for line in infile: if line[0] == '#' or line.isspace(): continue words = line.split() temp.append(float(words[0])) density.append(float(words[1])) return np.array(temp), np.array(density) if __name__ == "__main__": filename = "density_air.txt" #sys.argv[1] temp, dens = read_density_data(filename) plt.plot(temp, dens, 'ro') plt.show()