Plotting stochastic processes in Python

安稳与你 提交于 2021-02-04 12:33:36

问题


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?

                enter image description here

                                          enter image description here

                                enter image description here


回答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()

enter image description here



来源:https://stackoverflow.com/questions/19413016/plotting-stochastic-processes-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!