""" COMP 364 Lecture 15: Simple plots Author: Mathieu Perreault """ import matplotlib.pyplot as plt import sys, math # Generate some data points and put them in a file. Optional, could directly generate to lists. of = open('data.txt', 'w') for i in [x*0.1 for x in range(0,100)]: of.write("%s\t%s\n" %(i, math.sin(i))) of.close() # Simple chart x = [] y = [] for line in open('data.txt'): values = line.strip('\n').split() x.append(float(values[0])) y.append(float(values[1])) plt.figure() plt.plot(x,y, 'g.') plt.show() # Press the X button on this first chart to plot the second one. # Not so simple chart, filtering on the threshold (second argument) x = [] y = [] xb = [] yb = [] for line in open('data.txt'): values = line.strip('\n').split() yvalue = float(values[1]) if math.fabs(yvalue) < float(sys.argv[1]): x.append(float(values[0])) y.append(yvalue) else: xb.append(float(values[0])) yb.append(yvalue) plt.figure() plt.plot(x,y,'b.') plt.hold(True) plt.plot(xb,yb,'g.') # Further enhancements plt.title('Sin(x) function') plt.xlabel('x values') plt.ylabel('y values') plt.legend(['|sin(x)| < ' + sys.argv[1], '|sin(x)| >= ' + sys.argv[1]]) plt.show()