问题
Say I have a stochastic process defined between [0... N]
, e.g. N=50
. For every location, I have several samples (e.g. m=100
samples) (representing my sampling distribution at each location). One way to look at this is as a numpy 2D array of size (m,N)
.
How can I plot this intuitively in matplotlib
?
One possibility is to plot the process as a 1D plot along with an envelope of varying thickness and shade that captures the density of these distributions, something along the lines of what I show below. How can I do this in matplotlib
?
回答1:
For the first example, you can simply compute the percentiles at each fixed location, and then plot them using plt.fill_between
.
something like this
# Last-modified: 16 Oct 2013 05:08:28 PM
import numpy as np
import matplotlib.pyplot as plt
# generating fake data
locations = np.arange(0, 50, 1)
medians = locations/(1.0+(locations/5.0)**2)
disps = 0.1+0.5*locations/(1.0+(locations/5.0)**2.)
points = np.empty([50, 100])
for i in xrange(50) :
points[i,:] = np.random.normal(loc=medians[i], scale=disps[i], size=100)
# finding percentiles
pcts = np.array([20, 35, 45, 55, 65, 80])
layers = np.empty([50, 6])
for i in xrange(50) :
_sorted = np.sort(points[i,:])
layers[i, :] = _sorted[pcts]
# plot the layers
colors = ["blue", "green", "red", "green", "blue"]
for i in xrange(5) :
plt.fill_between(locations, layers[:, i], layers[:, i+1], color=colors[i])
plt.show()
来源:https://stackoverflow.com/questions/19413016/plotting-stochastic-processes-in-python