#! /usr/bin/env python3 # def python03 ( ): import matplotlib.pyplot as plt import numpy as np n = 11 x = np.linspace ( -2.0, 2.0, n ) y = x**2 + np.sin ( 53.0 * x ) plt.plot ( x, y ) filename = 'plot01.png' plt.show ( block = False ) plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Try again, with more points. Also, display the points with 'o' # n = 21 x = np.linspace ( -2.0, 2.0, n ) # because n changed, we have to recompute x y = x**2 + np.sin ( 53.0 * x ) # and then y plt.plot ( x, y, '-o' ) filename = 'plot02.png' plt.show ( block = False ) plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Again, but no connecting lines. # n = 101 x = np.linspace ( -2.0, 2.0, n ) y = x**2 + np.sin ( 53.0 * x ) plt.plot ( x, y, '.' ) plt.xlabel ( '<--- X axis --->' ) plt.ylabel ( '<--- Y = F(X) --->' ) filename = 'plot03.png' plt.show ( block = False ) plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # OK, 1001 points has got to be enough! # n = 1001 x = np.linspace ( -2.0, 2.0, n ) y = x**2 + np.sin ( 53.0 * x ) plt.plot ( x, y, '-' ) plt.grid ( True ) plt.title ( '1001 data points' ) filename = 'plot04.png' plt.show ( block = False ) plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Two curves on one plot. # n = 101 x = np.linspace ( 0.0, 2.0 * np.pi, n ) y1 = np.sin ( x ) y2 = np.cos ( x ) plt.plot ( x, y1, 'r-' ) # Draw this curve in Red plt.plot ( x, y2, 'g-' ) # Draw this curve in Green plt.grid ( True ) plt.title ( 'Trig fun' ) filename = 'plot05.png' plt.show ( block = False ) plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Draw a curve and save the plot in a file. # n = 101 x = np.linspace ( 0.0, 2.0, n ) y = 1.0 / ( ( x - 0.3 )**2 + 0.01 ) + 1.0 / ( ( x - 0.9 )**2 + 0.04 ) - 6.0 plt.plot ( x, y, 'r-' ) # Draw this curve in Red plt.plot ( [0.0, 2.0], [0.0,0.0], 'k-' ) # Black line from (0,0) to (2,0) plt.plot ( [0.0, 0.0], [0.0,100.0], 'k-' ) # Black line from (0,0) to (0,100) plt.grid ( True ) plt.title ( 'y = humps(x)' ) filename = 'humps.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( block = False ) plt.close ( ) # # Draw two plots in one figure. # x = np.linspace ( 0.0, 3.0, 101 ) y1 = np.cos(x)+5*np.cos(1.6*x)-2*np.cos(2*x)+5*np.cos(4.5*x)+7*np.cos(9*x) y2 = - np.sin(x)-8*np.sin(1.6*x)+4*np.sin(2*x)-22.5*np.sin(4.5*x)-63*np.sin(9*x) fig = plt.figure ( ) axes1 = fig.add_subplot ( 1, 2, 1 ) # on a (1x2) layout, this if the first subplot axes1.plot ( x, y1, 'r-' ) axes1.set_title ( 'y1=f(x)' ) axes1.grid ( True ) axes2 = fig.add_subplot ( 1, 2, 2 ) # and this is the second subplot axes2.plot ( x, y2, 'b-' ) axes2.set_title ( 'y2=dfdx(x)' ) axes2.grid ( True ) filename = 'two_plots.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( block = False ) plt.close ( ) # # Three plots. # data = np.loadtxt ( fname = 'inflammation-01.csv', delimiter = ',' ) y1 = np.max ( data, axis = 0 ) y2 = np.mean ( data, axis = 0 ) y3 = np.min ( data, axis = 0 ) fig = plt.figure ( ) axes1 = fig.add_subplot ( 1, 3, 1 ) # on a (1x3) layout, this if the first subplot axes1.plot ( y1, 'r-' ) axes1.set_title ( 'Maximum' ) axes1.grid ( True ) axes2 = fig.add_subplot ( 1, 3, 2 ) # and this is the second subplot axes2.plot ( y2, 'b-' ) axes2.set_title ( 'Mean' ) axes2.grid ( True ) axes3 = fig.add_subplot ( 1, 3, 3 ) # and this is the third subplot axes3.plot ( y3, 'g-' ) axes3.set_title ( 'Minimum' ) axes3.grid ( True ) filename = 'three_plots.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( block = False ) plt.close ( ) return if ( __name__ == "__main__" ): python03 ( )