Seite 1 von 1

Kurve fitten *matplotlib

Verfasst: Samstag 23. Mai 2020, 16:55
von ilonar
Moin!

Ich möchte gerne eine Kurve fitten und plotten. Leider bekomme ich dabei eine Fehlermeldung, die ich nicht ganz kapiere.

Hier ist mein Skript:

Code: Alles auswählen

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

def func(x, a, b, c):
    return a * np.exp(-b * x) + c

xdata = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
ydata = [1.638, 1.496, 1.392, 1.306, 1.232, 1.202, 1.171, 1.057]

popt, pcov = curve_fit(func, xdata, ydata)
popt

plt.plot(xdata, func(xdata, *popt), 'r-') 
 
Die Fehlermeldung kommt dann in der Zeile "return a * np.exp(-b * x) + c"

Code: Alles auswählen

TypeError: can't multiply sequence by non-int of type 'numpy.float64'

<Figure size 432x288 with 0 Axes>
Ich stehe gerade ein bisschen auf dem Schlauch, vielleicht weiß ja einer von euch weiter.
Danke sehr!

Viele Grüße
Ilona

Re: Kurve fitten *matplotlib

Verfasst: Samstag 23. Mai 2020, 18:06
von Sirius3
Schau Dir doch mal an, mit welchen Datentypen Du die Funktion `func` aufrufst, dann sollte es klar sein. Eine Liste kann man nicht mit einer Kommazahl multiplizieren (und das Multiplizieren mit einer Ganzzahl hat eine ganz andere Bedeutung).
Du willst wohl xdata in ein numpy-Array umwandeln.

Re: Kurve fitten *matplotlib

Verfasst: Samstag 23. Mai 2020, 18:23
von ilonar
Sirius3 hat geschrieben: Samstag 23. Mai 2020, 18:06 Schau Dir doch mal an, mit welchen Datentypen Du die Funktion `func` aufrufst, dann sollte es klar sein. Eine Liste kann man nicht mit einer Kommazahl multiplizieren (und das Multiplizieren mit einer Ganzzahl hat eine ganz andere Bedeutung).
Du willst wohl xdata in ein numpy-Array umwandeln.
ach.. klar! Danke dir!