如何在Python上绘制高斯函数? [英] How to plot a Gaussian function on Python?

查看:684
本文介绍了如何在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

每个频率与下面的IR强度相关,例如(频率= 95.1444/IR Inten = 4.5950),(频率= 208,5295/IR Inten = 0.1425)....依此类推.

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.

我必须在每个频率上构造一条高斯曲线,其高度为最强峰的相对强度.所有这些曲线的总和应该是红外光谱的模型.

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.

以下是一些提示:

高斯曲线为:

import math
y = a*math.exp(-(x-b)**2/(2*c*c))

其中

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()

推荐答案

您不必计算每个x和y值,可以通过这种方式计算均值和方差:

You don't have to compute every x and y values, you can do it in this way computing mean and variance:

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()

这篇关于如何在Python上绘制高斯函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆