[英]How to plot a Gaussian function on Python?
I have a datafile like this 我有一个这样的数据文件
Frequencies -- 95.1444 208.5295 256.0966
IR Inten -- 4.5950 0.1425 2.4807
Frequencies -- 273.7203 424.4748 446.9433
IR Inten -- 0.6420 0.0001 0.9654
Frequencies -- 520.5846 561.6770 630.1851
IR Inten -- 8.8996 6.4944 0.4674
Frequencies -- 703.7315 767.1711 799.2923
IR Inten -- 23.7514 63.4507 15.9273
Each frequency is related with the IR intensity below, for example (frequency= 95.1444/ IR Inten= 4.5950), (frequency= 208,5295/ IR Inten= 0.1425).... And so on. 每个频率与下面的IR强度相关,例如(频率= 95.1444 / IR Inten = 4.5950),(频率= 208,5295 / IR Inten = 0.1425)...等等。
I have to construct on every frequency a gaussian curve with height the relative intensity of the strongest peak. 我必须在每个频率上构造一个高斯曲线,其高度为最强峰的相对强度。 The sum of all those curves should be a model of the IR-spectrum.
所有这些曲线的总和应该是红外光谱的模型。
Here are some hints to do it: 以下是一些这样做的提示:
A gaussian curve is: 高斯曲线为:
import math
y = a*math.exp(-(x-b)**2/(2*c*c))
where 哪里
a: height of the peak
b: position of the center of the peak
c: controls the width of the peak
You can plot the function as follows: 您可以按以下方式绘制函数:
import pylab
pylab.plot(xs,ys)
# xs is a list of x-values
# ys is a list of y-values
pylab.show()
您可以使用以下库: import matplotlib.pyplot as plt
You don't have to compute every x and y values, you can do it in this way computing mean and variance: 您不必计算每个x和y值,可以通过这种方式计算均值和方差:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math
mu = 0
variance = 1
sigma = math.sqrt(variance)
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x,mlab.normpdf(x, mu, sigma))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.