简体   繁体   English

在python中绘制2D积分函数

[英]Plotting 2D integral function in python

Here is my first steps within the NumPy world. 这是我在NumPy世界中迈出的第一步。 As a matter of fact the target is plotting below 2-D function as a 3-D mesh: 实际上,目标是在2-D函数下方绘制为3-D网格:

N = \frac{n}{2\sigma\sqrt{\pi}}\exp^{-\frac{n^{2}x^{2}}{4\sigma^{2}}}

That could been done as a piece a cake in Matlab with below snippet: 这可以在Matlab中用下面的代码片段来完成:

[x,n] = meshgrid(0:0.1:20, 1:1:100);

mu = 0;
sigma = sqrt(2)./n;

f = normcdf(x,mu,sigma);
mesh(x,n,f);

But the bloody result is ugly enough to drive me trying Python capabilities to generate scientific plots. 但是流血的结果丑陋到足以迫使我尝试使用Python功能来生成科学图。

I searched something and found that the primary steps to hit above mark in Pyhton might be acquired by below snippet: 我搜索了一些内容,发现在Pyhton达到标高的主要步骤可能会被下面的代码段获取:

from matplotlib.patches import Polygon
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

sigma = 1

def integrand(x,n):
    return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2))

t = np.linespace(0, 20, 0.01)
n = np.linespace(1, 100, 1)

lower_bound = -100000000000000000000 #-inf
upper_bound = t

tt, nn = np.meshgrid(t,n)

real_integral = quad(integrand(tt,nn), lower_bound, upper_bound)

Axes3D.plot_trisurf(real_integral, tt,nn)

Edit: With due attention to more investigations on Greg's advices, above code is the most updated snippet. 编辑:在对Greg的建议进行更多研究后,上面的代码是最新的代码段。

Here is the generated exception: 这是生成的异常:

RuntimeError: infinity comparisons don't work for you

It is seemingly referring to the quad call... 看来是指quad通话...

Would you please helping me to handle this integrating-plotting problem?!... 您能帮我解决这个集成绘图问题吗?!...

Best 最好

Just a few hints to get you in the right direction. 只是一些提示,可以帮助您朝正确的方向发展。 numpy.meshgrid can do the same as MatLABs function: http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html numpy.meshgrid可以执行与MatLAB函数相同的功能: http ://docs.scipy.org/doc/numpy/reference/generation/numpy.meshgrid.html

When you have x and n you can do math just like in matlab: 当您拥有x和n时,可以像在matlab中那样进行数学运算:

sigma = numpy.sqrt(2)/n

(in python multiplication/division is default index by index - no dot needed) (在python中,乘法/除法是按索引的默认索引-不需要点)

scipy has a lot more advanced functions, see for example How to calculate cumulative normal distribution in Python for a 1D case. scipy具有许多更高级的功能,例如,参见如何针对一维情况在Python中计算累积正态分布

For plotting you can use matplotlibs pcolormesh: 对于绘图,您可以使用matplotlibs pcolormesh:

import matplotlib.pyplot as plt
plt.pcolormesh(x,n,real_integral)

Hope this helps until someone can give you a more detailed answer. 希望这会有所帮助,直到有人可以给您更详细的答案为止。

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

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