在Python中绘制随机过程 [英] Plotting stochastic processes in Python
问题描述
说我在 [0 ... N]
之间定义了一个随机过程,例如N=50
.对于每个位置,我都有几个样本(例如 m = 100
样本)(代表我在每个位置的采样分布).一种查看方式是大小为(m,N)
的numpy 2D数组.
如何在 matplotlib
中直观地绘制此图?
一种可能性是将过程绘制为一维图,以及一个不同厚度的信封和阴影来捕捉这些分布的密度,这与我下面展示的内容一致.如何在 matplotlib
中执行此操作?
对于第一个示例,您可以简单地计算每个固定位置的百分位数,然后使用 plt.fill_between
进行绘制./p>
类似的东西
# 最后修改时间:2013 年 10 月 16 日下午 05:08:28将numpy导入为np导入matplotlib.pyplot作为plt#生成虚假数据位置 = np.arange(0, 50, 1)中位数 = 位置/(1.0+(位置/5.0)**2)disps = 0.1+0.5*locations/(1.0+(locations/5.0)**2.)点数 = np.empty([50, 100])对于我在xrange(50)中:points [i ,:] = np.random.normal(loc = medians [i],scale = disps [i],size = 100)# 寻找百分位数pcts = np.array([20, 35, 45, 55, 65, 80])层数 = np.empty([50, 6])对于我在xrange(50)中:_sorted = np.sort(points[i,:])图层[i,:] = _sorted [pcts]# 绘制图层颜色 = [蓝"、绿"、红"、绿"、蓝"]对于我在xrange(5)中:plt.fill_between(locations, layers[:, i], layers[:, i+1], color=colors[i])plt.show()
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
?
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()
这篇关于在Python中绘制随机过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!