【问题标题】:Plotting histogram of brownian motion?绘制布朗运动的直方图?
【发布时间】:2015-07-08 18:05:57
【问题描述】:

我的任务是绘制布朗运动模拟的直方图。值得庆幸的是,我已经制作了一个模拟布朗运动的程序,并将其绘制在散点图上,作为时间和距离的函数。这是我的输出的样子:

但是,我需要将其转换为直方图,用于 5 个不同的位置(例如:t=0、1、2、3、4 处的直方图)。

目前我拥有的包是 numpy、mattplotlib 和 scipy。我已经看到了如何绘制正态分布的示例,但是如何为我收集的数据绘制分布?

这是我目前拥有的代码:http://pastebin.com/AEpQDQd2

(由于我无法发布两个链接,我不得不将两个文件粘贴在一起,第一个是计算布朗运动的文件,第二个是输出 imgur 链接上的图形的文件)

这不是家庭作业,而是作为额外的学分问题给出的,并且老师明确表示我们可以使用任何可以使用的信息。

谢谢。

【问题讨论】:

  • 请尝试详细说明您的编码问题是什么。你知道如何访问你想要绘制的数据吗? matplotlib的hist方法你看懂了吗?你能生成它的输入参数吗?你在什么时候卡住了?
  • 抱歉含糊不清。基本上我的问题是我不知道如何为我的特定数据使用直方图。我已经看到了几个如何绘制正态分布或二项分布的示例,但没有看到任何用户使用直方图绘制自己的数据的示例,而这正是我试图做的。

标签: python histogram physics


【解决方案1】:

使用pylab.hist这样的东西,计算出你感兴趣的时间点的索引后,it

# -*- coding: utf-8 -*-
import numpy
from pylab import plot, xlabel, ylabel, title, grid, show, hist, legend
from brownian import brownian

#This is the code that performs the main iteration of the Euler Marayuma process, from t=0 to t=10.

def main():

# The Wiener process parameter.
    delta = 2
    # Total time.
    T = 10.0
    # Number of steps.
    N = 500
    # Time step size
    dt = T/N
    # Number of realizations to generate.
    m = 1000
    # Create an empty array to store the realizations.
    x = numpy.empty((m,N+1))
    # Initial values of x.
    x[:, 0] = 0

    brownian(x[:,0], N, dt, delta, out=x[:,1:])
    t = numpy.linspace(0.0, N*dt, N+1)

    # time points of interest
    ts = [4., 3., 2., 1.]
    for t in ts:
        it = int(t/T * N)
        hist(x[:, it], alpha=0.7, label='%.1f s' % t, normed=True)
    legend()
    show()

if __name__ == "__main__":
    main()

(您可能会关心在单独的数字上绘制直方图和/或使 bin 大小在您的时间点上保持不变。)

【讨论】:

  • 确保对直方图进行归一化——直方图本身并没有提供任何信息(因为数量取决于 bin 的选择),而概率分布是相关的数量。
  • 这是一个很好的观点:应该将 normed=True 添加到对 hist 的调用中。
  • 非常感谢。这正如我所愿。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多