Python中的Gamma函数图
我想画一个简单的伽马函数,但我有一些问题。我的代码是:Python中的Gamma函数图,python,plot,scipy,matplotlib,Python,Plot,Scipy,Matplotlib,我想画一个简单的伽马函数,但我有一些问题。我的代码是: #!/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
#!/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函数,但出现以下错误:
回溯(最后一次调用):y1=f1(x)文件“D:/faxstuff/3.godina/kvantna/plotgamma.py”第13行的文件“D:/faxstuff/3.godina/kvantna/plotgamma.py”,第9行的f1返回gamma(x)文件“mtrand.pyx”,第1599行的mtrand.RandomState.gamma(numpy\random\mtrand\mtrand.c:8389)ValueError:shape模块之一(我想是pylab)通过gamma随机变量函数对gamma函数进行阴影处理。这是可行的,但我必须关闭对legend的呼叫(我还不知道为什么)
在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()
请发布准确的错误消息。
Traceback(最近一次调用最后一次):y1=f1(x)文件“D:/faxstuff/3.godina/kvantna/plotgamma.py”第13行,y1=f1(x)文件“D:/faxstuff/3.godina/kvantna/plotgamma.py”,f1返回伽马(x)文件“mtrand.pyx”第1599行,mtrand.rand.rand.rand.rand.c:8389)ValueError:shape这是一个很好的例子,说明了为什么不建议使用import*
。需要将其更改为:图例([r'$\Gamma(x)$)),这是一个很好的例子,说明了为什么不建议使用“from foo import*”。
# 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()