"""Ex. 5.17 in "A primer on... " Read data from file and plot it. The exercise does not require writing a function, but this is convenient since we can then reuse the function in ex 5.19. """ import matplotlib.pyplot as plt import sys import numpy as np def read_density_data(filename): temp = [] density = [] with open(filename,'r') 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) """This if-test ensures that the following lines are only run if the code is run as a stand-alone program, and not if it is imported as a module into another program.""" if __name__ == "__main__": filename = sys.argv[1] temp, dens = read_density_data(filename) plt.plot(temp,dens,'o') plt.show() """ Terminal> python read_density_data.py density_air.txt (output is a plot) """