Now, we will read the data from the text file. This is a good place to demonstrate the while
loop, since we won't always know in advance how many lines are in the file. Run the following code to load the data.
from matplotlib import pyplot as plt import os path = '/Users/cfinch/Documents/Writing/Sage for Beginners/Chapters/Chapter 4/' fileName = 'data.txt' # Read in the data file times = [] data = [] text_file = open(os.path.join(path, fileName), 'r') line = text_file.readline() while len(line) > 0: print(line) # split each line into a list of strings elements = line.split(',') # Strip newlines and convert strings to real numbers times.append(float(elements[0].strip())) data.append(float(elements[1].strip())) line = text_file.readline() text_file.close() # Plot the data plt.figure() plt.plot(times, data) plt.savefig('example2b.png') plt.close()
The data is plotted to another image file. This plot should...