Python 中的伽玛函数图
所以我想绘制简单的伽玛函数,但我有一些问题。我的代码是:
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中一个模块(我认为是 pylab)是通过 gamma 随机变量函数来隐藏 gamma 函数。这可行,但我必须关闭对图例的调用(我还不知道为什么)。
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).
在 Sage 笔记本中尝试一下:
Try this in a Sage notebook: