import matplotlib.pyplot as plt import numpy as np import matplotlib.cbook as cbook
# load up some sample financial data with cbook.get_sample_data('goog.npz') as datafile: r = np.load(datafile)['price_data'].view(np.recarray) # Matplotlib prefers datetime instead of np.datetime64. date = r.date.astype('O') # create two subplots with the shared x and y axes fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
# an (Nsteps x Nwalkers) array of random walk steps S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers) S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
# an (Nsteps x Nwalkers) array of random walker positions X1 = S1.cumsum(axis=0) X2 = S2.cumsum(axis=0)
# Nsteps length arrays empirical means and standard deviations of both # populations over time mu1 = X1.mean(axis=1) sigma1 = X1.std(axis=1) mu2 = X2.mean(axis=1) sigma2 = X2.std(axis=1)
# here we use the where argument to only fill the region where the # walker is above the population 1 sigma boundary ax.fill_between(t, upper_bound, X, where=X > upper_bound, facecolor='blue', alpha=0.5) ax.set_xlabel('num steps') ax.set_ylabel('position') ax.grid()