用Python绘制随机过程

时间:2013-10-16 20:17:35

标签: python numpy matplotlib pandas scipy

假设我在[0... N]之间定义了一个随机过程,例如N=50。对于每个位置,我都有几个样本(例如m=100个样本)(代表我在每个位置的采样分布)。一种看待这种情况的方法是作为一个大小为(m,N)的numpy二维数组。

如何在matplotlib中直观地绘制?

一种可能性是将过程绘制为一维图以及不同厚度的包络和阴影,以捕获这些分布的密度,这与我在下面显示的内容一致。我怎样才能在matplotlib

中执行此操作

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:9)

对于第一个示例,您可以简单地计算每个固定位置的百分位数,然后使用plt.fill_between绘制它们。

类似这样的事情

# 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

相关问题