# This program will make plots in python, version 3+, using # utilities from matplotlib and numpy. # Always include these next two import commands. import numpy as np import matplotlib.pyplot as plt # This shows how to define your own function. def func(x): return np.sin(2*np.pi*x) # ***** How to make a plot of data with error bars. ****** # Generate some fake data. x1 = np.arange(10) + 2*np.random.randn(10) y1 = np.arange(10) + 2*np.random.randn(10) # Generate fake errors for the data. x1err = 2*np.random.random(10) y1err = 2*np.random.random(10) # Make the plot. plt.errorbar(x1, y1, xerr=x1err, yerr=y1err, fmt='bo') plt.xlabel('X Data', labelpad=10) plt.ylabel('Y Data', labelpad=10) plt.title('Random Plot with Error Bars') plt.show() # ***** How to make a plot of multiple functions. ***** # Now make another plot with multiple plots on the same # screen. x2 = np.arange(1, 19, .4) y2a = np.log10(x2) y2b = 0.01 * x2**2 y2c = 0.9 * np.sin(x2) plt.plot(x2, y2a, 'r-', label='y2a') plt.plot(x2, y2b, 'b^', label='y2b') plt.plot(x2, y2c, 'go', label='y2c') plt.plot(x2, y2a+y2b+y2c, '+', label='y2a+y2b+y2c') plt.legend(loc=2) plt.show() # ***** How to make more than one plot on a page. ***** # We'll make use of of the defined function `func' from # the beginning of this Python program. x3a = np.arange(0.0, 4.0, 0.1) x3b = np.arange(0.0, 4.0, 0.01) y3a = func(x3a) y3b = func(x3b) y3an = y3a + 0.1*np.random.randn(len(x3a)) plt.figure() # Initialize the figure space. # Make 2 plots vertically + 1 horizontally. # Start with the first (upper left). plt.subplot(211) plt.plot(x3a, y3a, 'bo', x3b, y3b, 'r:') plt.subplot(212) # Now the 2nd (lower left) plot. plt.plot(x3a, y3an, 'bo', x3b, y3b, 'r:') plt.show()