Python Forum
Returning values from Gaussian fitting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning values from Gaussian fitting
#1
Hey,

I'm trying to fit a Gaussian function to some data, but I want to return the 'center' and 'width' values and print them. The code returns a ValueError:

import numpy as np
import matplotlib.pyplot as plt
import pathlib
import os
from scipy.optimize import curve_fit

#Data
data = np.loadtxt('....file.hst')
x = data[:,0]
y1 = data[:,1]
y2 = data[:,2]

n_gauss = 1

offset = 0

def gaussian(x, offset, area, center, width):
    y = area * np.exp(-(x - center)**2 / (2 * width**2)) + offset
    return y, center, width

def multi_gaussian(x, *gaussians):
    y = gaussians[0] * np.ones_like(x)
    n_gauss = (len(gaussians)-1) // 3
    for gauss in range(n_gauss):
        y += gaussian(x, offset, gaussians[3*gauss+1], gaussians[3*gauss+2],
                      gaussians[3*gauss+3])
    return y
    
#Plots:
plt.plot(x, y1, label="sync1")
plt.plot(x, y2, label="sync2")
popt, pcov = curve_fit(gaussian, x, y1, p0=[0, max(y1), 900, 120])
#popt, pcov = curve_fit(gaussian, x, y2, p0=[5000, max(y2), center, width])
#plt.plot(x, gaussian(y1, -2100, 3200, center, width),
#    lw=1, c='m', ls='--', label='Gaussian')
plt.plot(x, multi_gaussian(y1, -210, 270, 900, 120),
         lw=1, c='r', ls='--', label='multi-Gaussian')
#plt.plot(x, gaussian(y2, 0, 2250, 900, 120),
#    lw=1, c='r', ls='--', label='Gaussian')
#plt.xlim([850, 980])
#plt.ylim([30, 600])
plt.legend()

#Outputs
#print("Center: ", center)
#print("Width: ", width)
The error is this:

    return func(xdata, *params) - ydata

ValueError: operands could not be broadcast together with shapes (3,) (1201,) 
and there is thus no plot. Removing 'center' and 'width' from the return line in the Gaussian function solves the problem, but I don't know how to get those values out from the fitting otherwise. Any advice?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fitting data to a curve daaegp 1 567 Jun-30-2023, 08:06 PM
Last Post: Gribouillis
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Fitting Gaussian curve to data file Laplace12 0 2,672 Jun-09-2021, 10:45 AM
Last Post: Laplace12
  returning values in for loop Nickd12 4 11,861 Dec-17-2020, 03:51 AM
Last Post: snippsat
  search of a curve fitting function bluffy5 2 2,377 Dec-13-2020, 09:53 AM
Last Post: ndc85430
  Rotate 2D Gaussian given parameters schniefen 4 2,844 Dec-11-2020, 03:34 PM
Last Post: schniefen
  Multi-gaussian function Laplace12 5 3,925 Jul-21-2020, 11:38 PM
Last Post: scidam
  Scipy kolmogorov smirnov test for evaluating the fitting of a non-normal distribution mcva 0 1,954 May-26-2020, 12:01 PM
Last Post: mcva
  How do I apply a Gaussian blur to a particular edge of geometry in Matplotlib? hbolandi 0 1,950 Feb-02-2020, 06:08 PM
Last Post: hbolandi
  gaussian quadrature mohammadrezamofidi 4 4,048 Jun-18-2019, 04:06 PM
Last Post: michalmonday

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020