Python 中的伽玛函数图

发布于 2024-11-02 02:23:12 字数 757 浏览 3 评论 0原文

所以我想绘制简单的伽玛函数,但我有一些问题。我的代码是:

#!/usr/bin/env python
# -*- coding: cp1250 -*-
#import math
from scipy.special import *
#from scitools.std import *
from pylab import *

def f1(x):
    return gamma(x)


x = linspace(-6, 6, 512)
y1 = f1(x)

# Matlab-style syntax:
plot(x, y1)

xlabel('x')
ylabel('y')
legend(r'$\Gamma(x)$')
grid(True)

show()

我尝试从 math 和 scipy.special 导入 gamma 函数,但出现以下错误:

回溯(最近一次调用最后一次):文件“D:/faxstuff/3.godina/kvantna/plotgamma.py”,第 13 行,在 y1 = f1(x) 文件“D:/faxstuff/3.godina/”中kvantna/plotgamma.py”,第 9 行,在 f1 返回 gamma(x) 文件“mtrand.pyx”,第 1599 行,在mtrand.RandomState.gamma (numpy\random\mtrand\mtrand.c:8389) ValueError: 形状 <= 0

如何做到这一点?这应该很容易,但我似乎失败了:(

So I'd like to plot simple gamma function, but I have some problems. My code is:

#!/usr/bin/env python
# -*- coding: cp1250 -*-
#import math
from scipy.special import *
#from scitools.std import *
from pylab import *

def f1(x):
    return gamma(x)


x = linspace(-6, 6, 512)
y1 = f1(x)

# Matlab-style syntax:
plot(x, y1)

xlabel('x')
ylabel('y')
legend(r'$\Gamma(x)

I tried importing the gamma function from math, and from scipy.special but I get the following error:

Traceback (most recent call last): File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 13, in y1 = f1(x) File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 9, in f1 return gamma(x) File "mtrand.pyx", line 1599, in mtrand.RandomState.gamma (numpy\random\mtrand\mtrand.c:8389) ValueError: shape <= 0

How to do it? This should be easy, but I seem to fail :(

) grid(True) show()

I tried importing the gamma function from math, and from scipy.special but I get the following error:

Traceback (most recent call last): File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 13, in y1 = f1(x) File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 9, in f1 return gamma(x) File "mtrand.pyx", line 1599, in mtrand.RandomState.gamma (numpy\random\mtrand\mtrand.c:8389) ValueError: shape <= 0

How to do it? This should be easy, but I seem to fail :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦归所梦 2024-11-09 02:23:12

其中一个模块(我认为是 pylab)是通过 gamma 随机变量函数来隐藏 gamma 函数。这可行,但我必须关闭对图例的调用(我还不知道为什么)。

from scipy.special import gamma as Gamma
#from scitools.std import *
from pylab import *

def f1(x):
    return Gamma(x)


x = linspace(-6, 6, 512)
y1 = f1(x)
gca().set_autoscale_on(False)

# Matlab-style syntax:
plot(x, y1)

xlabel('x')
ylabel('y')
# legend(r'$\Gamma(x)
)
axis([-6, 6, -100, 100])
grid(True)

show()

One of the modules (pylab, I think) is shadowing the gamma function by the gamma random variable function. This works, but I had to turn off the call to legend (I'm not sure why, yet).

from scipy.special import gamma as Gamma
#from scitools.std import *
from pylab import *

def f1(x):
    return Gamma(x)


x = linspace(-6, 6, 512)
y1 = f1(x)
gca().set_autoscale_on(False)

# Matlab-style syntax:
plot(x, y1)

xlabel('x')
ylabel('y')
# legend(r'$\Gamma(x)
)
axis([-6, 6, -100, 100])
grid(True)

show()
反差帅 2024-11-09 02:23:12

在 Sage 笔记本中尝试一下:

# Simple example demonstrating how to interact with matplotlib directly.
# Comment plt.clf() to get the plots overlay in each update.

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

@interact
def plot_gamma(a=(1,(1,10)), loc=(0,(0,10)), scale=(1,(1,10))):
    rv = stats.gamma(a, loc, scale)
    x = np.linspace(-1,20,1000)
    plt.plot(x,rv.pdf(x))
    plt.grid(True)
    plt.savefig('plt.png')
    plt.clf()

Try this in a Sage notebook:

# Simple example demonstrating how to interact with matplotlib directly.
# Comment plt.clf() to get the plots overlay in each update.

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

@interact
def plot_gamma(a=(1,(1,10)), loc=(0,(0,10)), scale=(1,(1,10))):
    rv = stats.gamma(a, loc, scale)
    x = np.linspace(-1,20,1000)
    plt.plot(x,rv.pdf(x))
    plt.grid(True)
    plt.savefig('plt.png')
    plt.clf()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文