简体   繁体   English

Plot 使用 matplotlib (Python) 中的颜色网格的随机轨迹偏离“真实”路径

[英]Plot stochastic trajectories deviations from 'real' path using a colormesh in matplotlib (Python)

Hi I created a program that will create deviations from a real trajectory, it is complicated and I do not have a simple example unfortunately.嗨,我创建了一个程序,它会偏离真实的轨迹,它很复杂,不幸的是我没有一个简单的例子。

It calculates a path with stochastic initial conditions from the real path and does this for x iterations, the goal is to show that the deviations become larger at greater times.它从真实路径计算具有随机初始条件的路径,并针对 x 次迭代执行此操作,目标是表明偏差在更多时间变得更大。

The real path and the deviations are showed below.真实路径和偏差如下所示。 噪音

However I want to show that the deviations become greater the longer in time we are.然而,我想表明,我们的时间越长,偏差就会越大。 Ofcourse I could just calculate the variance and plot mean+var and mean-var at each time step but I was wondering if I could plot something like this, using hist2d当然我可以在每个时间步计算方差和 plot mean+var 和 mean-var 但我想知道我是否可以使用 hist2d 像这样的 plot

hist2d

You see that the blocks are not as smooth as a like and this is not that great to use.您会看到这些块不像类似的那样光滑,而且使用起来也不是很好。

Then I went and looked at python's kde and created the following.然后我去看了python的kde并创建了以下内容。

在此处输入图像描述

This is also not preferable as I think it bins more points at the minima and maxima.这也不是可取的,因为我认为它在最小值和最大值处合并了更多点。 Also it is 'too smeared out'.它也“太模糊了”。 Especially in the beginning, all the points are the same so I want there just to be a straight line to really show that the deviations start later on.尤其是在开始的时候,所有的点都是一样的,所以我希望有一条直线来真正表明偏差是在以后开始的。

I guess my question is;我想我的问题是; is what I want even possible and what package/command should I use.是我想要的,我应该使用什么包/命令。 I haven't found what I am looking for on other questions.我还没有找到我在寻找其他问题的内容。 Or has anyone a suggestion to nicely show what I want in a any other way?或者有没有人建议以任何其他方式很好地展示我想要的东西?

Here is an idea plotting multiple curves with transparency on top of each other:这是一个在彼此之上绘制具有透明度的多条曲线的想法:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)
for _ in range(1000):
    plt.plot(x, np.sin(x * np.random.normal(1, 0.1)) * np.random.normal(1, 0.1), color='r', alpha=0.02)
plt.plot(x, np.sin(x), color='b')
plt.margins(x=0)
plt.show()

具有透明度的多条曲线

Another option creates a 2d histogram:另一个选项创建一个 2d 直方图:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)
all_curves = np.array([np.sin(x * np.random.normal(1, 0.1)) * np.random.normal(1, 0.1) for _ in range(100)])
plt.hist2d(x=np.tile(x, all_curves.shape[0]), y=all_curves.ravel(), bins=(100, 100), cmap='inferno')
plt.show()

二维直方图

Still another approach would use fill_between (as suggested by @bramb) between confidence intervals:还有一种方法是在置信区间之间使用fill_between (如@bramb 所建议):

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)
all_curves = np.array([np.sin(x * np.random.normal(1, 0.1)) * np.random.normal(1, 0.1) for _ in range(1000)])

confidence_interval1 = 95
confidence_interval2 = 80
confidence_interval3 = 50
for ci in [confidence_interval1, confidence_interval2, confidence_interval3]:
    low = np.percentile(all_curves, 50 - ci / 2, axis=0)
    high = np.percentile(all_curves, 50 + ci / 2, axis=0)
    plt.fill_between(x, low, high, color='r', alpha=0.2)
plt.plot(x, np.sin(x), color='b')
plt.margins(x=0)
plt.show()

fill_between 置信区间

You could use something like the matplotlib.pyplot.fill_between method.您可以使用类似matplotlib.pyplot.fill_between方法的东西。 It fills everything between y1 (max) and y2 (min) for a given (common) x array.对于给定的(公共)x 数组,它会填充 y1(最大值)和 y2(最小值)之间的所有内容。 You would then be able to accentuate that the filled region keeps enlarging with increasing x value.然后,您将能够强调填充区域随着 x 值的增加而不断扩大。

However, this would require you to find the minimal and maximal value of your deviations at each time point and save these to two separate arrays.但是,这需要您找到每个时间点的偏差的最小值和最大值,并将它们保存到两个单独的 arrays。 The exact method of doing this will depend on how you are storing these individual runs.执行此操作的确切方法将取决于您如何存储这些单独的运行。

In case they are separate lists / arrays, you can convert these to a numpy matrix / pandas dataframe and use the minimum / maximum methods along the relevant axis. In case they are separate lists / arrays, you can convert these to a numpy matrix / pandas dataframe and use the minimum / maximum methods along the relevant axis.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM