0

I have this code, the txt data is not important. the thing that puzzles me is why does it work with the *popt in plt.plot. but it doesnt when I take that symbol out

import numpy as np

import matplotlib.pyplot as plt 

import scipy.optimize as sc 


fecha, temperatura = np.loadtxt('munich_temperatures_average_with_bad_data.txt', unpack=True)


keep = np.abs(temperatura) < 90


fecha = fecha[keep]

temperatura = temperatura[keep]

def cosine(x, a, b, c):
    return a * np.cos(2 * np.pi * x + b) + c


popt, pcov = sc.curve_fit(cosine, fecha, temperatura)

plt.plot(fecha, temperatura, '.')

xfine = np.linspace(1995, 2014, 100)

plt.plot(xfine, cosine(xfine, *popt), color='red', lw=2)

the error TypeError: cosine() missing 2 required positional arguments: 'b' and 'c' appears when * is not on the plt.plot

Fcatalan
  • 3
  • 1
  • `cosine(xfine, *popt)` expands to `cosine(xfine, popt[0], popt[1], popt[2])`, matching the signature `cosine(x, a, b, c)`. – hpaulj Apr 04 '23 at 16:15
  • Does this answer your question? [What do \*\* (double star/asterisk) and \* (star/asterisk) mean in a function call?](https://stackoverflow.com/questions/2921847/what-do-double-star-asterisk-and-star-asterisk-mean-in-a-function-call) – Reinderien Apr 05 '23 at 00:50

0 Answers0