如何在绘图数据上标记拟合高斯曲线所需的位置?

时间:2020-03-03 13:42:12

标签: python matplotlib lmfit

我正在尝试通过用户标记背景点的方式将高斯lmfit模型拟合到我的数据中,然后当他按下名为“ fit”的按钮时,程序会将曲线拟合到他标记的位置(类似于enter image description here <-在这里包括的图片并打印拟合报告 这是我的代码:

import matplotlib.pyplot as plt

import numpy as np
import pandas as pd
from lmfit.models import GaussianModel 
from matplotlib.widgets import Cursor, Button



x, y = np.loadtxt("fily3.txt", skiprows=1, unpack = True) 



mod2 = GaussianModel()

pars = mod2.guess(y, x=x)  #Estimate initial model parameter values from data
out = mod2.fit(y, pars, x=x) #Fit the model to the data using the supplied Parameters
fig, ax = plt.subplots()

print("Gaussian fit report \n")
print(out.fit_report(min_correl=0.25))

plt.plot(x, y, 'b')
plt.plot(x, out.best_fit, 'k--', label='best fit')
cursor = Cursor(ax,
            horizOn=True, # Controls the visibility of the horizontal line
            vertOn=True, # Controls the visibility of the vertical line
            color='green',
            linewidth=2.0
            )
def onclick(event):
x1, y1 = event.xdata, event.ydata
print("X-Coordinate :  ",x1," & ", "Y-Coordinate :  ", y1)
fig.canvas.mpl_connect('button_press_event', onclick)
plt.title(" Gaussian model")
plt.xlabel("2-Theta")
plt.ylabel("Intensity")
plt.grid(True)
plt.show()

`

0 个答案:

没有答案