guess function when using built-in defined models in lmfit - python
I am having a problem with the guess function of lmfit. I am trying to fit some experimental data and I want to use different built in models of lmfit, but I cannot run the built in modules, only if I define the function directly.
The following code does not work, but if I comment the guess function it works.
P.S. It would be more interesting for me that the index is the first column because I will put this in a loop that will use all the same first column of the data and therefore i could put each new second column of the data as a new column in the DataFrame.
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
from lmfit.models import GaussianModel
minvalue = 3.25
maxvalue = 3.45
rawdata = pd.read_csv('datafile.txt', delim_whitespace = True, names=['XX','YY'])
#Section the data
data = rawdata[(rawdata['XX']>minvalue) & (rawdata['XX'] < maxvalue)]
#Create a DataFrame with the data
dataDataframe = pd.DataFrame()
dataDataframe[0] = data['YY']
dataDataframe = dataDataframe.set_index(data['XX'])
# Gaussian curve
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(np.sqrt(2*np.pi)*wid)) * np.exp(-(x-cen)**2 /(2*wid**2))
result_gaussian = Model(gaussian).fit(dataDataframe[0], x=dataDataframe.index.values, amp=5, cen=5, wid=1)
mod = GaussianModel()
pars = mod.guess(dataDataframe[0], x = np.float32(dataDataframe.index.values))
out = mod.fit(dataDataframe[0], pars , x = np.float32(dataDataframe.index.values))
plt.plot(dataDataframe.index.values, dataDataframe[0],'bo')
plt.plot(dataDataframe.index.values, result_gaussian.best_fit, 'r-', label = 'Gaussian')
plt.plot(dataDataframe.index.values, out.best_fit, 'b-', label = 'Gaussian2')
plt.legend()
plt.show()
Error message I am having if I uncomment the built in modules:
File "/Users/johndoe/anaconda2/lib/python2.7/site-packages/lmfit/models.py", line 52, in guess_from_peak
cen = x[imaxy]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I have tried to run the guess_from_peak from models.py and i did not have a problem it resulted in an integer.
Raw data:
1.1661899e+000 7.3414581e+002
1.1730889e+000 7.4060590e+002
1.1799880e+000 7.3778076e+002
1.1868871e+000 7.2950366e+002
1.1937861e+000 7.0154932e+002
1.2006853e+000 7.0399518e+002
1.2075844e+000 7.3814081e+002
1.2144834e+000 7.5750049e+002
1.2213825e+000 7.6613043e+002
1.2282816e+000 7.4348322e+002
1.2351807e+000 7.2836584e+002
1.2420797e+000 7.0964618e+002
1.2489789e+000 7.1938611e+002
1.2558780e+000 7.0620062e+002
1.2627770e+000 7.2354883e+002
1.2696761e+000 7.1347961e+002
1.2765752e+000 7.1027679e+002
1.2834742e+000 7.4422925e+002
1.2903733e+000 7.5596112e+002
1.2972724e+000 7.2770599e+002
1.3041714e+000 7.2000342e+002
1.3110706e+000 7.4451556e+002
1.3179697e+000 7.4411346e+002
1.3248687e+000 6.9408307e+002
1.3317678e+000 6.8662170e+002
1.3386669e+000 7.0951758e+002
1.3455659e+000 6.7616663e+002
1.3524650e+000 6.7230786e+002
1.3593642e+000 7.1053870e+002
1.3662632e+000 7.2593860e+002
1.3731623e+000 7.1484381e+002
1.3800614e+000 7.3073920e+002
1.3869605e+000 7.2766406e+002
1.3938595e+000 7.1958862e+002
1.4007586e+000 7.0147577e+002
1.4076577e+000 6.9747528e+002
1.4145567e+000 6.9634515e+002
1.4214559e+000 6.6082648e+002
1.4283550e+000 6.4877466e+002
1.4352540e+000 6.6942896e+002
1.4421531e+000 6.8172211e+002
1.4490522e+000 6.5540350e+002
1.4559512e+000 6.4846545e+002
1.4628503e+000 6.6383038e+002
1.4697495e+000 6.4449670e+002
1.4766484e+000 6.3950043e+002
1.4835476e+000 6.4479529e+002
1.4904467e+000 6.4849249e+002
1.4973457e+000 6.4100800e+002
1.5042448e+000 6.6731049e+002
1.5111439e+000 6.8118671e+002
1.5180429e+000 6.5618878e+002
1.5249420e+000 6.3446680e+002
1.5318412e+000 6.3301892e+002
1.5387402e+000 6.5466571e+002
1.5456393e+000 6.5982983e+002
1.5525384e+000 6.3588879e+002
1.5594375e+000 6.1257922e+002
1.5663365e+000 6.2805811e+002
1.5732356e+000 6.1877094e+002
1.5801347e+000 6.0427368e+002
1.5870337e+000 6.3391718e+002
1.5939329e+000 6.4173145e+002
1.6008320e+000 6.2423242e+002
1.6077310e+000 6.0993829e+002
1.6146301e+000 6.0605164e+002
1.6215292e+000 6.2812646e+002
1.6284282e+000 6.4028595e+002
1.6353273e+000 6.2281421e+002
1.6422265e+000 6.0742285e+002
1.6491255e+000 5.9783905e+002
1.6560246e+000 5.8637256e+002
1.6629237e+000 6.0021320e+002
1.6698227e+000 6.1169287e+002
1.6767218e+000 6.1003906e+002
1.6836209e+000 5.9548285e+002
1.6905199e+000 5.8961163e+002
1.6974190e+000 5.9599597e+002
1.7043182e+000 5.9016595e+002
1.7112173e+000 5.7669794e+002
1.7181163e+000 5.6394800e+002
1.7250154e+000 5.5043781e+002
1.7319145e+000 5.6813892e+002
1.7388135e+000 5.8987500e+002
1.7457126e+000 5.9018683e+002
1.7526118e+000 5.8595575e+002
1.7595108e+000 5.8304041e+002
1.7664099e+000 5.9360785e+002
1.7733090e+000 5.9706018e+002
1.7802080e+000 5.7838733e+002
1.7871071e+000 5.7011194e+002
1.7940062e+000 5.8080725e+002
1.8009052e+000 5.7853046e+002
1.8078043e+000 5.7998969e+002
1.8147035e+000 5.4928967e+002
1.8216025e+000 5.2888440e+002
1.8285016e+000 5.4854303e+002
1.8354007e+000 5.5585767e+002
1.8422997e+000 5.5588806e+002
1.8491988e+000 5.5359229e+002
1.8560979e+000 5.5033203e+002
1.8629971e+000 5.2563916e+002
1.8698961e+000 5.3607788e+002
1.8767952e+000 5.7113812e+002
1.8836943e+000 5.5775525e+002
1.8905933e+000 5.2081384e+002
1.8974924e+000 5.1039877e+002
1.9043915e+000 5.3863855e+002
1.9112905e+000 5.6284332e+002
1.9181896e+000 5.5691626e+002
1.9250888e+000 5.3292615e+002
1.9319878e+000 5.4550836e+002
1.9388869e+000 5.6732916e+002
1.9457860e+000 5.4372571e+002
1.9526850e+000 5.1244263e+002
1.9595841e+000 5.1212933e+002
1.9664832e+000 5.1553162e+002
1.9733822e+000 5.2064484e+002
1.9802814e+000 5.3102246e+002
1.9871805e+000 5.2069739e+002
1.9940795e+000 5.0833780e+002
2.0009787e+000 5.1853204e+002
2.0078776e+000 5.2843738e+002
2.0147767e+000 5.2046942e+002
2.0216758e+000 5.4993433e+002
2.0285749e+000 5.4103894e+002
2.0354741e+000 5.0149301e+002
2.0423732e+000 5.0521149e+002
2.0492721e+000 5.2875800e+002
2.0561712e+000 5.1962280e+002
2.0630703e+000 4.9481357e+002
2.0699694e+000 4.9459094e+002
2.0768685e+000 4.9837778e+002
2.0837677e+000 4.9990302e+002
2.0906668e+000 4.9616635e+002
2.0975657e+000 4.9398682e+002
2.1044648e+000 4.9411301e+002
2.1113639e+000 5.0085464e+002
2.1182630e+000 5.1741498e+002
2.1251621e+000 5.1049081e+002
2.1320612e+000 4.9854333e+002
2.1389601e+000 4.9250342e+002
2.1458592e+000 4.8195938e+002
2.1527584e+000 4.9623288e+002
2.1596575e+000 5.0226831e+002
2.1665566e+000 5.1108215e+002
2.1734557e+000 5.0001602e+002
2.1803546e+000 4.8078720e+002
2.1872537e+000 4.9371985e+002
2.1941528e+000 4.9578796e+002
2.2010520e+000 5.0061276e+002
2.2079511e+000 4.9850949e+002
2.2148502e+000 4.9680969e+002
2.2217491e+000 5.0683179e+002
2.2286482e+000 5.0175012e+002
2.2355473e+000 4.8996030e+002
2.2424464e+000 4.8759747e+002
2.2493455e+000 4.7695905e+002
2.2562447e+000 4.7682187e+002
2.2631438e+000 4.8609653e+002
2.2700427e+000 4.8575693e+002
2.2769418e+000 4.9476901e+002
2.2838409e+000 4.8241449e+002
2.2907400e+000 4.7581494e+002
2.2976391e+000 5.0079959e+002
2.3045382e+000 5.0975296e+002
2.3114371e+000 4.9256650e+002
2.3183362e+000 4.8954599e+002
2.3252354e+000 4.9478619e+002
2.3321345e+000 5.1234747e+002
2.3390336e+000 5.4276178e+002
2.3459327e+000 5.4188184e+002
2.3528316e+000 5.4555566e+002
2.3597307e+000 5.4856274e+002
2.3666298e+000 5.2246918e+002
2.3735290e+000 4.9281882e+002
2.3804281e+000 4.8422125e+002
2.3873272e+000 5.0562274e+002
2.3942261e+000 5.0024243e+002
2.4011252e+000 4.8827591e+002
2.4080243e+000 4.8137762e+002
2.4149234e+000 4.7244000e+002
2.4218225e+000 4.7699164e+002
2.4287217e+000 4.7515668e+002
2.4356208e+000 4.6413528e+002
2.4425197e+000 4.6328885e+002
2.4494188e+000 4.6013199e+002
2.4563179e+000 4.6177853e+002
2.4632170e+000 4.5766202e+002
2.4701161e+000 4.4741263e+002
2.4770153e+000 4.4859024e+002
2.4839141e+000 4.6913116e+002
2.4908133e+000 5.0019971e+002
2.4977124e+000 4.8486560e+002
2.5046115e+000 4.6070554e+002
2.5115106e+000 4.3163672e+002
2.5184097e+000 4.4147137e+002
2.5253086e+000 4.3510056e+002
2.5322077e+000 4.4211298e+002
2.5391068e+000 4.6599957e+002
2.5460060e+000 4.5878577e+002
2.5529051e+000 4.4981293e+002
2.5598042e+000 4.6061084e+002
2.5667033e+000 4.6963638e+002
2.5736022e+000 4.7663760e+002
2.5805013e+000 4.6380307e+002
2.5874004e+000 4.5866577e+002
2.5942996e+000 4.5507098e+002
2.6011987e+000 4.4790939e+002
2.6080978e+000 4.6447559e+002
2.6149967e+000 4.5061194e+002
2.6218958e+000 4.2355850e+002
2.6287949e+000 4.2002722e+002
2.6356940e+000 4.2429697e+002
2.6425931e+000 4.2280334e+002
2.6494923e+000 4.3304733e+002
2.6563911e+000 4.5999661e+002
2.6632903e+000 4.7144125e+002
2.6701894e+000 4.6819211e+002
2.6770885e+000 4.6265125e+002
2.6839876e+000 4.6332251e+002
2.6908867e+000 4.5123907e+002
2.6977856e+000 4.6259286e+002
2.7046847e+000 4.6975299e+002
2.7115839e+000 4.4647833e+002
2.7184830e+000 4.4722562e+002
2.7253821e+000 4.6617062e+002
2.7322812e+000 4.6656949e+002
2.7391803e+000 4.4081876e+002
2.7460792e+000 4.5200452e+002
2.7529783e+000 4.5094382e+002
2.7598774e+000 4.4421115e+002
2.7667766e+000 4.5470145e+002
2.7736757e+000 4.5202261e+002
2.7805748e+000 4.4788058e+002
2.7874737e+000 4.3493640e+002
2.7943728e+000 4.4102286e+002
2.8012719e+000 4.3156961e+002
2.8081710e+000 4.2983533e+002
2.8150702e+000 4.4627554e+002
2.8219693e+000 4.4581104e+002
2.8288682e+000 4.2150226e+002
2.8357673e+000 4.1737479e+002
2.8426664e+000 4.5602731e+002
2.8495655e+000 4.6227423e+002
2.8564646e+000 4.5953806e+002
2.8633637e+000 4.5829834e+002
2.8702629e+000 4.5450616e+002
2.8771617e+000 4.5531360e+002
2.8840609e+000 4.4464761e+002
2.8909600e+000 4.6128970e+002
2.8978591e+000 4.4664514e+002
2.9047582e+000 4.4719708e+002
2.9116573e+000 4.4492749e+002
2.9185562e+000 4.4260013e+002
2.9254553e+000 4.5593594e+002
2.9323545e+000 4.6237164e+002
2.9392536e+000 4.7034845e+002
2.9461527e+000 4.7368185e+002
2.9530518e+000 4.7302234e+002
2.9599507e+000 4.7327332e+002
2.9668498e+000 4.4960791e+002
2.9737489e+000 4.4319986e+002
2.9806480e+000 4.5416092e+002
2.9875472e+000 4.6674429e+002
2.9944463e+000 4.6089871e+002
3.0013452e+000 4.6334650e+002
3.0082443e+000 4.6833719e+002
3.0151434e+000 4.8842966e+002
3.0220425e+000 4.8455182e+002
3.0289416e+000 4.6504678e+002
3.0358407e+000 4.6673508e+002
3.0427399e+000 4.6887064e+002
3.0496387e+000 4.6799823e+002
3.0565379e+000 4.5299500e+002
3.0634370e+000 4.5381485e+002
3.0703361e+000 4.5956931e+002
3.0772352e+000 4.6477676e+002
3.0841343e+000 4.6114374e+002
3.0910332e+000 4.6816293e+002
3.0979323e+000 4.6245181e+002
3.1048315e+000 4.6533044e+002
3.1117306e+000 4.7819165e+002
3.1186297e+000 4.9699246e+002
3.1255288e+000 4.8907956e+002
3.1324277e+000 4.9116394e+002
3.1393268e+000 5.0308936e+002
3.1462259e+000 5.0668982e+002
3.1531250e+000 5.0537222e+002
3.1600242e+000 4.9574966e+002
3.1669233e+000 4.9894128e+002
3.1738222e+000 4.9885315e+002
3.1807213e+000 5.1417163e+002
3.1876204e+000 5.2202740e+002
3.1945195e+000 5.2219598e+002
3.2014186e+000 5.4433679e+002
3.2083178e+000 5.6957477e+002
3.2152169e+000 5.9891089e+002
3.2221158e+000 6.0682019e+002
3.2290149e+000 6.0779541e+002
3.2359140e+000 6.1212280e+002
3.2428131e+000 6.5589185e+002
3.2497122e+000 7.1807507e+002
3.2566113e+000 7.5950916e+002
3.2635102e+000 8.1842242e+002
3.2704093e+000 9.1277783e+002
3.2773085e+000 1.0486207e+003
3.2842076e+000 1.3214080e+003
3.2911067e+000 1.7085295e+003
3.2980058e+000 2.4946370e+003
3.3049047e+000 4.1229609e+003
3.3118038e+000 7.1944038e+003
3.3187029e+000 1.1714122e+004
3.3256021e+000 1.5338923e+004
3.3325012e+000 1.5092694e+004
3.3394003e+000 1.1227008e+004
3.3462994e+000 6.9070176e+003
3.3531983e+000 4.0318586e+003
3.3600974e+000 2.5069387e+003
3.3669965e+000 1.7313556e+003
3.3738956e+000 1.3203175e+003
3.3807948e+000 1.0810967e+003
3.3876939e+000 9.2702356e+002
3.3945928e+000 8.2453217e+002
3.4014919e+000 7.5468195e+002
3.4083910e+000 7.1011224e+002
3.4152901e+000 6.7312701e+002
3.4221892e+000 6.2927734e+002
3.4290884e+000 6.0679126e+002
3.4359872e+000 5.8445929e+002
3.4428864e+000 5.5084033e+002
3.4497855e+000 5.2990625e+002
3.4566846e+000 5.3244171e+002
3.4635837e+000 5.3299860e+002
3.4704828e+000 5.2270801e+002
3.4773817e+000 5.0838147e+002
3.4842808e+000 4.9768036e+002
3.4911799e+000 4.9974271e+002
3.4980791e+000 5.1852539e+002
3.5049782e+000 5.2486890e+002
3.5118773e+000 5.3554919e+002
3.5187764e+000 5.4363098e+002
3.5256753e+000 5.2134320e+002
3.5325744e+000 4.9386557e+002
3.5394735e+000 4.7175720e+002
3.5463727e+000 4.6334061e+002
3.5532718e+000 4.4633063e+002
3.5601709e+000 4.4021204e+002
3.5670698e+000 4.4216010e+002
3.5739689e+000 4.3208749e+002
3.5808680e+000 4.3210999e+002
3.5877671e+000 4.3717999e+002
3.5946662e+000 4.3084845e+002
3.6015654e+000 4.1379028e+002
3.6084642e+000 4.1567856e+002
3.6153634e+000 4.2414615e+002
3.6222625e+000 4.2964746e+002
3.6291616e+000 4.1986203e+002
3.6360607e+000 4.0300714e+002
3.6429598e+000 4.1156561e+002
3.6498590e+000 4.1897156e+002
3.6567578e+000 4.1506668e+002
3.6636569e+000 4.2337305e+002
3.6705561e+000 4.2956845e+002
3.6774552e+000 4.1608209e+002
3.6843543e+000 4.1159943e+002
3.6912534e+000 4.0408707e+002
3.6981523e+000 3.8742813e+002
3.7050514e+000 3.8193686e+002
3.7119505e+000 3.8675006e+002
3.7188497e+000 3.8995547e+002
3.7257488e+000 3.9189124e+002
3.7326479e+000 3.9534134e+002
3.7395468e+000 4.0249893e+002
3.7464459e+000 4.0382443e+002
3.7533450e+000 3.9881796e+002
3.7602441e+000 4.0283856e+002
3.7671432e+000 4.0544543e+002
3.7740424e+000 3.9527063e+002
3.7809412e+000 3.9659631e+002
3.7878404e+000 4.0054132e+002
3.7947395e+000 3.9123737e+002
3.8016386e+000 3.8058502e+002
3.8085377e+000 3.7388980e+002
3.8154368e+000 3.7337103e+002
3.8223360e+000 3.6008588e+002
3.8292348e+000 3.5135416e+002
3.8361340e+000 3.5958188e+002
3.8430331e+000 3.5756583e+002
3.8499322e+000 3.5956232e+002
3.8568313e+000 3.7803802e+002
3.8637304e+000 3.9012396e+002
3.8706293e+000 3.8674255e+002
3.8775284e+000 3.7771600e+002
3.8844275e+000 3.7648160e+002
3.8913267e+000 3.7692780e+002
3.8982258e+000 3.6927103e+002
3.9051249e+000 3.7007745e+002
3.9120238e+000 3.7482629e+002
3.9189229e+000 3.7230219e+002
3.9258220e+000 3.6110025e+002
3.9327211e+000 3.6490872e+002
3.9396203e+000 3.7283734e+002
3.9465194e+000 3.7933209e+002
3.9534183e+000 3.6968182e+002
3.9603174e+000 3.5532330e+002
3.9672165e+000 3.5889478e+002
3.9741156e+000 3.6407483e+002
3.9810147e+000 3.6295535e+002
3.9879138e+000 3.6387720e+002
3.9948130e+000 3.6416183e+002
4.0017118e+000 3.6089911e+002
4.0086112e+000 3.6826599e+002
4.0155101e+000 3.7570581e+002
4.0224090e+000 3.6361679e+002
4.0293083e+000 3.6003177e+002
4.0362072e+000 3.7528265e+002
4.0431066e+000 3.7368362e+002
4.0500054e+000 3.8174683e+002
4.0569048e+000 4.0386084e+002
4.0638037e+000 4.2738324e+002
4.0707026e+000 4.4587668e+002
4.0776019e+000 4.5433987e+002
4.0845008e+000 4.4404083e+002
4.0914001e+000 4.2589066e+002
4.0982990e+000 3.9662262e+002
4.1051979e+000 3.7311325e+002
4.1120973e+000 3.5790594e+002
4.1189961e+000 3.4554794e+002
4.1258955e+000 3.5435367e+002
4.1327944e+000 3.7766489e+002
4.1396937e+000 3.7425708e+002
4.1465926e+000 3.5805182e+002
4.1534915e+000 3.5078519e+002
4.1603909e+000 3.5888739e+002
4.1672897e+000 3.7242688e+002
4.1741891e+000 3.7792575e+002
4.1810880e+000 3.7338031e+002
4.1879873e+000 3.6538324e+002
4.1948862e+000 3.5872525e+002
4.2017851e+000 3.4688391e+002
4.2086844e+000 3.4881918e+002
4.2155833e+000 3.4818274e+002
4.2224827e+000 3.4055273e+002
4.2293816e+000 3.3977536e+002
4.2362804e+000 3.3322891e+002
4.2431798e+000 3.3594962e+002
4.2500787e+000 3.4658536e+002
4.2569780e+000 3.4479083e+002
4.2638769e+000 3.4267456e+002
4.2707763e+000 3.4828876e+002
4.2776752e+000 3.4845041e+002
4.2845740e+000 3.3986469e+002
4.2914734e+000 3.3093433e+002
4.2983723e+000 3.3255331e+002
4.3052716e+000 3.4089511e+002
4.3121705e+000 3.4742932e+002
4.3190699e+000 3.3570422e+002
4.3259687e+000 3.2636673e+002
4.3328676e+000 3.3228806e+002
4.3397670e+000 3.5141977e+002
4.3466659e+000 3.5683167e+002
4.3535652e+000 3.4719943e+002
4.3604641e+000 3.4054718e+002
4.3673630e+000 3.2842471e+002
4.3742623e+000 3.2503146e+002
4.3811612e+000 3.3431540e+002
4.3880606e+000 3.3462808e+002
4.3949594e+000 3.3529224e+002
4.4018588e+000 3.3313510e+002
4.4087577e+000 3.4015598e+002
4.4156566e+000 3.3703552e+002
4.4225559e+000 3.3024448e+002
4.4294548e+000 3.2974786e+002
As I suggested in the comment above, coercing the pandas Series into an ndarray will fix the problem:
mod = GaussianModel()
ydata = np.array(dataDataframe[0])
xdata = np.array(dataDataframe.index.values)
pars = mod.guess(ydata, x=xdata)
out = mod.fit(ydata, pars, x=xdata)
This example works for me:
#!/usr/bin/env python
from lmfit.models import LorentzianModel
import matplotlib.pyplot as plt
import pandas as pd
dframe = pd.read_csv('peak.csv')
model = LorentzianModel()
params = model.guess(dframe['y'], x=dframe['x'])
result = model.fit(dframe['y'], params, x=dframe['x'])
print(result.fit_report())
result.plot_fit()
plt.show()
with peaks.csv of
x,y
0.000000, 0.021654
0.200000, 0.385367
0.400000, 0.193304
0.600000, 0.103481
0.800000, 0.404041
1.000000, 0.212585
1.200000, 0.253212
1.400000, -0.037306
1.600000, 0.271415
1.800000, 0.025614
2.000000, 0.066419
2.200000, -0.034347
2.400000, 0.153702
2.600000, 0.161341
2.800000, -0.097676
3.000000, -0.061880
3.200000, 0.085341
3.400000, 0.083674
3.600000, 0.190944
3.800000, 0.222168
4.000000, 0.214417
4.200000, 0.341221
4.400000, 0.634501
4.600000, 0.302566
4.800000, 0.101096
5.000000, -0.106441
5.200000, 0.567396
5.400000, 0.531899
5.600000, 0.459800
5.800000, 0.646655
6.000000, 0.662228
6.200000, 0.820844
6.400000, 0.947696
6.600000, 1.541353
6.800000, 1.763981
7.000000, 1.846081
7.200000, 2.986333
7.400000, 3.182907
7.600000, 3.786487
7.800000, 4.822287
8.000000, 5.739122
8.200000, 6.744448
8.400000, 7.295213
8.600000, 8.737766
8.800000, 9.693782
9.000000, 9.894218
9.200000, 10.193956
9.400000, 10.091519
9.600000, 9.652392
9.800000, 8.670938
10.000000, 8.004205
10.200000, 6.773599
10.400000, 6.076502
10.600000, 5.127315
10.800000, 4.303762
11.000000, 3.426006
11.200000, 2.416431
11.400000, 2.311363
11.600000, 1.748020
11.800000, 1.135594
12.000000, 0.888514
12.200000, 1.030794
12.400000, 0.543024
12.600000, 0.767751
12.800000, 0.657551
13.000000, 0.495730
13.200000, 0.447520
13.400000, 0.173839
13.600000, 0.256758
13.800000, 0.596106
14.000000, 0.065328
14.200000, 0.197267
14.400000, 0.260038
14.600000, 0.460880
14.800000, 0.335248
15.000000, 0.295977
15.200000, -0.010228
15.400000, 0.138670
15.600000, 0.192113
15.800000, 0.304371
16.000000, 0.442517
16.200000, 0.164944
16.400000, 0.001907
16.600000, 0.207504
16.800000, 0.012640
17.000000, 0.090878
17.200000, -0.222967
17.400000, 0.391717
17.600000, 0.180295
17.800000, 0.206875
18.000000, 0.240595
18.200000, -0.037437
18.400000, 0.139918
18.600000, 0.012560
18.800000, -0.053009
19.000000, 0.226069
19.200000, 0.076879
19.400000, 0.078599
19.600000, 0.016125
19.800000, -0.071217
20.000000, -0.091474
Related
Smooth the stair-like surface plot in matplotlib
how can I smooth / extrapolate surface plot to avoid stair-like jumps in the envelope? import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import griddata import pandas as pd df = pd.read_csv("data.csv", header=None, delimiter=";", na_values=[None], names=["X", "Y", "Z"]) df['Z'] = df['Z'].astype(float) X = df["X"].unique() Y = df["Y"].unique() XX, YY = np.meshgrid(X, Y) ZZ = df["Z"].to_numpy().reshape(Y.size, X.size) fig, ax = plt.subplots(1, 1) ax.contourf(XX, YY, ZZ, 25, cmap='jet') plt.show() Here is the data: 0;0;0 100;0;0 200;0;0 300;0;0 400;0;0 500;0;0 600;0;0 700;0;0 800;0;0 900;0;0 1000;0;0 1100;0;0 1200;0;0 1300;0;0 1400;0;0 1500;0;0 1600;0;0 1700;0;0 1800;0;0 1900;0;0 2000;0;0 2100;0;0 2200;0;0 2300;0;0 2400;0;0 2500;0;0 2600;0;0 2700;0;0 2800;0;0 2900;0;0 3000;0;0 3100;0;0 3200;0;0 3300;0;0 3400;0;0 3500;0;0 3600;0;0 3700;0;0 3800;0;0 3900;0;0 4000;0;0 4100;0;0 4200;0;0 4300;0;0 4400;0;0 4500;0;0 4600;0;0 4700;0;0 4800;0;0 4900;0;0 5000;0;0 5100;0;0 5200;0;0 5300;0;0 5400;0;0 5500;0;0 5600;0;0 5700;0;0 5800;0;0 5900;0;0 6000;0;0 6100;0;0 6200;0;0 6300;0;0 6400;0;0 6500;0;0 6600;0;0 6700;0;0 6800;0;0 6900;0;0 7000;0;0 7100;0;0 7200;0;0 7300;0;0 7400;0;0 7500;0;0 7600;0;0 7700;0;0 7800;0;0 7900;0;0 8000;0;0 8100;0;0 8200;0;0 8300;0;0 8400;0;0 8500;0;0 8600;0;0 8700;0;0 8800;0;0 8900;0;0 9000;0;0 9100;0;0 9200;0;0 9300;0;0 9400;0;0 9500;0;0 9600;0;0 9700;0;0 9800;0;0 9900;0;0 10000;0;0 0;5;0 100;5;43.775 200;5;60.729 300;5;69.712 400;5;75.267 500;5;79.037 600;5;81.759 700;5;83.815 800;5;85.42 900;5;86.706 1000;5;87.759 1100;5;88.636 1200;5;89.376 1300;5;90.009 1400;5;90.555 1500;5;91.031 1600;5;91.448 1700;5;91.817 1800;5;92.146 1900;5;92.439 2000;5;92.702 2100;5;92.94 2200;5;93.155 2300;5;93.35 2400;5;93.528 2500;5;93.691 2600;5;93.839 2700;5;93.976 2800;5;94.102 2900;5;94.218 3000;5;94.325 3100;5;94.424 3200;5;94.516 3300;5;94.601 3400;5;94.679 3500;5;94.753 3600;5;94.821 3700;5;94.884 3800;5;94.943 3900;5;94.998 4000;5;95.05 4100;5;95.098 4200;5;95.142 4300;5;95.184 4400;5;95.223 4500;5;95.259 4600;5;95.293 4700;5;95.324 4800;5;95.354 4900;5;95.381 5000;5;95.407 5100;5;95.43 5200;5;95.452 5300;5;95.473 5400;5;95.492 5500;5;95.509 5600;5;95.526 5700;5;95.541 5800;5;95.554 5900;5;95.567 6000;5;95.579 6100;5;95.589 6200;5;95.599 6300;5;95.602 6400;5;95.573 6500;5;95.511 6600;5;95.428 6700;5;95.325 6800;5;95.208 6900;5;95.074 7000;5;94.925 7100;5;94.779 7200;5;94.619 7300;5;94.443 7400;5;94.278 7500;5;94.103 7600;5;93.929 7700;5;93.748 7800;5;93.577 7900;5;93.365 8000;5;93.212 8100;5;93.044 8200;5;92.832 8300;5;92.678 8400;5;92.511 8500;5;92.365 8600;5;92.23 8700;5;92.025 8800;5;91.908 8900;5;91.668 9000;5;91.529 9100;5;91.461 9200;5;91.308 9300;5;91.147 9400;5;90.975 9500;5;90.885 9600;5;90.695 9700;5;90.594 9800;5;90.383 9900;5;90.27 10000;5;90.221 0;10;0 100;10;28.723 200;10;44.58 300;10;54.629 400;10;61.562 500;10;66.634 600;10;70.502 700;10;73.55 800;10;76.012 900;10;78.041 1000;10;79.743 1100;10;81.189 1200;10;82.433 1300;10;83.515 1400;10;84.463 1500;10;85.301 1600;10;86.047 1700;10;86.715 1800;10;87.316 1900;10;87.859 2000;10;88.353 2100;10;88.804 2200;10;89.217 2300;10;89.597 2400;10;89.946 2500;10;90.27 2600;10;90.569 2700;10;90.848 2800;10;91.107 2900;10;91.349 3000;10;91.576 3100;10;91.788 3200;10;91.987 3300;10;92.174 3400;10;92.35 3500;10;92.516 3600;10;92.673 3700;10;92.821 3800;10;92.961 3900;10;93.094 4000;10;93.22 4100;10;93.34 4200;10;93.454 4300;10;93.562 4400;10;93.665 4500;10;93.763 4600;10;93.857 4700;10;93.946 4800;10;94.032 4900;10;94.113 5000;10;94.191 5100;10;94.266 5200;10;94.338 5300;10;94.406 5400;10;94.472 5500;10;94.535 5600;10;94.595 5700;10;94.653 5800;10;94.709 5900;10;94.755 6000;10;94.774 6100;10;94.767 6200;10;94.735 6300;10;94.69 6400;10;94.636 6500;10;94.562 6600;10;94.475 6700;10;94.377 6800;10;94.297 6900;10;94.193 7000;10;94.084 7100;10;93.976 7200;10;93.867 7300;10;93.753 7400;10;93.641 7500;10;93.518 7600;10;93.428 7700;10;93.308 7800;10;93.198 7900;10;93.056 8000;10;92.983 8100;10;92.88 8200;10;92.768 8300;10;92.648 8400;10;92.519 8500;10;92.459 8600;10;92.313 8700;10;92.295 8800;10;92.168 8900;10;92.053 9000;10;91.968 9100;10;91.878 9200;10;91.782 9300;10;91.639 9400;10;91.64 9500;10;91.529 9600;10;91.483 9700;10;91.358 9800;10;91.303 9900;10;91.245 10000;10;91.1 0;15;0 100;15;21.556 200;15;35.446 300;15;45.139 400;15;52.286 500;15;57.772 600;15;62.115 700;15;65.639 800;15;68.554 900;15;71.005 1000;15;73.095 1100;15;74.897 1200;15;76.467 1300;15;77.848 1400;15;79.07 1500;15;80.16 1600;15;81.137 1700;15;82.019 1800;15;82.819 1900;15;83.547 2000;15;84.212 2100;15;84.823 2200;15;85.385 2300;15;85.904 2400;15;86.385 2500;15;86.831 2600;15;87.247 2700;15;87.635 2800;15;87.998 2900;15;88.338 3000;15;88.658 3100;15;88.958 3200;15;89.241 3300;15;89.508 3400;15;89.76 3500;15;89.999 3600;15;90.225 3700;15;90.44 3800;15;90.644 3900;15;90.838 4000;15;91.022 4100;15;91.198 4200;15;91.366 4300;15;91.526 4400;15;91.68 4500;15;91.826 4600;15;91.966 4700;15;92.101 4800;15;92.229 4900;15;92.353 5000;15;92.472 5100;15;92.586 5200;15;92.695 5300;15;92.801 5400;15;92.902 5500;15;92.997 5600;15;93.055 5700;15;93.075 5800;15;93.077 5900;15;93.061 6000;15;93.007 6100;15;92.952 6200;15;92.878 6300;15;92.809 6400;15;92.724 6500;15;92.641 6600;15;92.542 6700;15;92.458 6800;15;92.316 6900;15;92.252 7000;15;92.124 7100;15;92.014 7200;15;91.915 7300;15;91.781 7400;15;91.701 7500;15;91.57 7600;15;91.47 7700;15;91.355 7800;15;91.229 7900;15;91.127 8000;15;91.038 8100;15;90.884 8200;15;90.817 8300;15;90.642 8400;15;90.562 8500;15;90.475 8600;15;90.379 8700;15;90.23 8800;15;90.119 8900;15;90.001 9000;15;89.876 9100;15;89.83 9200;15;89.692 9300;15;89.545 9400;15;89.485 9500;15;89.324 9600;15;89.247 9700;15;89.053 9800;15;88.962 9900;15;88.748 10000;15;88.641 0;20;0 100;20;17.143 200;20;29.257 300;20;38.271 400;20;45.238 500;20;50.783 600;20;55.302 700;20;59.054 800;20;62.22 900;20;64.925 1000;20;67.265 1100;20;69.307 1200;20;71.106 1300;20;72.701 1400;20;74.127 1500;20;75.407 1600;20;76.564 1700;20;77.614 1800;20;78.572 1900;20;79.448 2000;20;80.253 2100;20;80.995 2200;20;81.681 2300;20;82.317 2400;20;82.908 2500;20;83.46 2600;20;83.975 2700;20;84.457 2800;20;84.909 2900;20;85.334 3000;20;85.735 3100;20;86.112 3200;20;86.469 3300;20;86.806 3400;20;87.126 3500;20;87.429 3600;20;87.717 3700;20;87.99 3800;20;88.251 3900;20;88.5 4000;20;88.737 4100;20;88.963 4200;20;89.179 4300;20;89.387 4400;20;89.585 4500;20;89.775 4600;20;89.957 4700;20;90.132 4800;20;90.3 4900;20;90.462 5000;20;90.617 5100;20;90.767 5200;20;90.906 5300;20;90.993 5400;20;91.04 5500;20;91.041 5600;20;91.013 5700;20;90.958 5800;20;90.904 5900;20;90.8 6000;20;90.7 6100;20;90.594 6200;20;90.513 6300;20;90.36 6400;20;90.236 6500;20;90.093 6600;20;89.929 6700;20;89.818 6800;20;89.661 6900;20;89.517 7000;20;89.297 7100;20;89.158 7200;20;88.937 7300;20;88.768 7400;20;88.581 7500;20;88.372 7600;20;88.198 7700;20;87.954 7800;20;87.785 7900;20;87.402 8000;20;87.261 8100;20;86.931 8200;20;86.571 8300;20;86.149 8400;20;85.651 8500;20;84.934 8600;20;NaN 8700;20;NaN 8800;20;NaN 8900;20;NaN 9000;20;NaN 9100;20;NaN 9200;20;NaN 9300;20;NaN 9400;20;NaN 9500;20;NaN 9600;20;NaN 9700;20;NaN 9800;20;NaN 9900;20;NaN 10000;20;NaN 0;25;0 100;25;14.542 200;25;24.631 300;25;32.887 400;25;39.506 500;25;44.932 600;25;49.46 700;25;53.295 800;25;56.585 900;25;59.439 1000;25;61.937 1100;25;64.142 1200;25;66.103 1300;25;67.858 1400;25;69.437 1500;25;70.866 1600;25;72.165 1700;25;73.351 1800;25;74.438 1900;25;75.438 2000;25;76.361 2100;25;77.215 2200;25;78.008 2300;25;78.746 2400;25;79.434 2500;25;80.078 2600;25;80.681 2700;25;81.247 2800;25;81.78 2900;25;82.282 3000;25;82.756 3100;25;83.204 3200;25;83.628 3300;25;84.031 3400;25;84.412 3500;25;84.775 3600;25;85.121 3700;25;85.45 3800;25;85.763 3900;25;86.063 4000;25;86.35 4100;25;86.623 4200;25;86.886 4300;25;87.137 4400;25;87.378 4500;25;87.61 4600;25;87.832 4700;25;88.046 4800;25;88.251 4900;25;88.449 5000;25;88.597 5100;25;88.662 5200;25;88.669 5300;25;88.641 5400;25;88.564 5500;25;88.454 5600;25;88.343 5700;25;88.173 5800;25;87.978 5900;25;87.779 6000;25;87.619 6100;25;87.334 6200;25;87.094 6300;25;86.786 6400;25;86.432 6500;25;86.029 6600;25;85.728 6700;25;85.226 6800;25;84.582 6900;25;83.788 7000;25;82.493 7100;25;NaN 7200;25;NaN 7300;25;NaN 7400;25;NaN 7500;25;NaN 7600;25;NaN 7700;25;NaN 7800;25;NaN 7900;25;NaN 8000;25;NaN 8100;25;NaN 8200;25;NaN 8300;25;NaN 8400;25;NaN 8500;25;NaN 8600;25;NaN 8700;25;NaN 8800;25;NaN 8900;25;NaN 9000;25;NaN 9100;25;NaN 9200;25;NaN 9300;25;NaN 9400;25;NaN 9500;25;NaN 9600;25;NaN 9700;25;NaN 9800;25;NaN 9900;25;NaN 10000;25;NaN 0;30;0 100;30;11.622 200;30;20.82 300;30;28.279 400;30;34.45 500;30;39.64 600;30;44.065 700;30;47.882 800;30;51.209 900;30;54.134 1000;30;56.725 1100;30;59.037 1200;30;61.112 1300;30;62.985 1400;30;64.684 1500;30;66.232 1600;30;67.648 1700;30;68.949 1800;30;70.147 1900;30;71.255 2000;30;72.282 2100;30;73.236 2200;30;74.126 2300;30;74.957 2400;30;75.736 2500;30;76.466 2600;30;77.152 2700;30;77.798 2800;30;78.408 2900;30;78.984 3000;30;79.529 3100;30;80.045 3200;30;80.536 3300;30;81.001 3400;30;81.444 3500;30;81.866 3600;30;82.268 3700;30;82.653 3800;30;83.019 3900;30;83.37 4000;30;83.706 4100;30;84.028 4200;30;84.337 4300;30;84.633 4400;30;84.918 4500;30;85.191 4600;30;85.454 4700;30;85.684 4800;30;85.875 4900;30;85.931 5000;30;85.873 5100;30;85.747 5200;30;85.589 5300;30;85.309 5400;30;84.994 5500;30;84.567 5600;30;84.204 5700;30;83.5 5800;30;82.652 5900;30;81.491 6000;30;NaN 6100;30;NaN 6200;30;NaN 6300;30;NaN 6400;30;NaN 6500;30;NaN 6600;30;NaN 6700;30;NaN 6800;30;NaN 6900;30;NaN 7000;30;NaN 7100;30;NaN 7200;30;NaN 7300;30;NaN 7400;30;NaN 7500;30;NaN 7600;30;NaN 7700;30;NaN 7800;30;NaN 7900;30;NaN 8000;30;NaN 8100;30;NaN 8200;30;NaN 8300;30;NaN 8400;30;NaN 8500;30;NaN 8600;30;NaN 8700;30;NaN 8800;30;NaN 8900;30;NaN 9000;30;NaN 9100;30;NaN 9200;30;NaN 9300;30;NaN 9400;30;NaN 9500;30;NaN 9600;30;NaN 9700;30;NaN 9800;30;NaN 9900;30;NaN 10000;30;NaN 0;35;0 100;35;96.527 200;35;17.603 300;35;24.265 400;35;29.928 500;35;34.8 600;35;39.036 700;35;42.754 800;35;46.042 900;35;48.971 1000;35;51.596 1100;35;53.963 1200;35;56.107 1300;35;58.059 1400;35;59.843 1500;35;61.481 1600;35;62.988 1700;35;64.381 1800;35;65.671 1900;35;66.871 2000;35;67.987 2100;35;69.03 2200;35;70.006 2300;35;70.922 2400;35;71.782 2500;35;72.592 2600;35;73.355 2700;35;74.077 2800;35;74.759 2900;35;75.406 3000;35;76.019 3100;35;76.602 3200;35;77.156 3300;35;77.684 3400;35;78.188 3500;35;78.668 3600;35;79.127 3700;35;79.566 3800;35;79.986 3900;35;80.389 4000;35;80.775 4100;35;81.146 4200;35;81.502 4300;35;81.844 4400;35;82.173 4500;35;82.49 4600;35;82.636 4700;35;82.522 4800;35;82.302 4900;35;81.917 5000;35;81.253 5100;35;80.269 5200;35;78.316 5300;35;NaN 5400;35;NaN 5500;35;NaN 5600;35;NaN 5700;35;NaN 5800;35;NaN 5900;35;NaN 6000;35;NaN 6100;35;NaN 6200;35;NaN 6300;35;NaN 6400;35;NaN 6500;35;NaN 6600;35;NaN 6700;35;NaN 6800;35;NaN 6900;35;NaN 7000;35;NaN 7100;35;NaN 7200;35;NaN 7300;35;NaN 7400;35;NaN 7500;35;NaN 7600;35;NaN 7700;35;NaN 7800;35;NaN 7900;35;NaN 8000;35;NaN 8100;35;NaN 8200;35;NaN 8300;35;NaN 8400;35;NaN 8500;35;NaN 8600;35;NaN 8700;35;NaN 8800;35;NaN 8900;35;NaN 9000;35;NaN 9100;35;NaN 9200;35;NaN 9300;35;NaN 9400;35;NaN 9500;35;NaN 9600;35;NaN 9700;35;NaN 9800;35;NaN 9900;35;NaN 10000;35;NaN 0;40;0 100;40;80.252 200;40;14.856 300;40;20.741 400;40;25.863 500;40;30.362 600;40;34.344 700;40;37.893 800;40;41.077 900;40;43.949 1000;40;46.553 1100;40;48.924 1200;40;51.092 1300;40;53.083 1400;40;54.917 1500;40;56.611 1600;40;58.182 1700;40;59.642 1800;40;61.002 1900;40;62.273 2000;40;63.462 2100;40;64.578 2200;40;65.627 2300;40;66.615 2400;40;67.546 2500;40;68.427 2600;40;69.26 2700;40;70.049 2800;40;70.798 2900;40;71.51 3000;40;72.187 3100;40;72.832 3200;40;73.447 3300;40;74.035 3400;40;74.596 3500;40;75.133 3600;40;75.647 3700;40;76.139 3800;40;76.612 3900;40;77.065 4000;40;77.501 4100;40;77.92 4200;40;78.323 4300;40;78.496 4400;40;78.403 4500;40;NaN 4600;40;NaN 4700;40;NaN 4800;40;NaN 4900;40;NaN 5000;40;NaN 5100;40;NaN 5200;40;NaN 5300;40;NaN 5400;40;NaN 5500;40;NaN 5600;40;NaN 5700;40;NaN 5800;40;NaN 5900;40;NaN 6000;40;NaN 6100;40;NaN 6200;40;NaN 6300;40;NaN 6400;40;NaN 6500;40;NaN 6600;40;NaN 6700;40;NaN 6800;40;NaN 6900;40;NaN 7000;40;NaN 7100;40;NaN 7200;40;NaN 7300;40;NaN 7400;40;NaN 7500;40;NaN 7600;40;NaN 7700;40;NaN 7800;40;NaN 7900;40;NaN 8000;40;NaN 8100;40;NaN 8200;40;NaN 8300;40;NaN 8400;40;NaN 8500;40;NaN 8600;40;NaN 8700;40;NaN 8800;40;NaN 8900;40;NaN 9000;40;NaN 9100;40;NaN 9200;40;NaN 9300;40;NaN 9400;40;NaN 9500;40;NaN 9600;40;NaN 9700;40;NaN 9800;40;NaN 9900;40;NaN 10000;40;NaN
Use SmoothBivariateSpline to interpolate NAN values. import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import SmoothBivariateSpline import pandas as pd df = pd.read_csv("data.csv", header=None, delimiter=";", na_values=[None], names=["X", "Y", "Z"]) df['Z'] = df['Z'].astype(float) x = df["X"] y = df["Y"] z = df["Z"] xnew = np.unique(x) ynew = np.unique(y) mask = ~np.isnan(z) x=(x[mask]) y=(y[mask]) z=(z[mask]) f = SmoothBivariateSpline(x,y,z,kx=1,ky=1) znew=np.transpose(f(xnew, ynew)) fig, ax = plt.subplots(1, 1) ax.contourf(xnew, ynew, znew, 25, cmap='jet') plt.show() You get the figure :
TypeError when fitting Statsmodels OLS with standard errors clustered 2 ways
Context Building on top of How to run Panel OLS regressions with 3+ fixed-effect and errors clustering? and notably Josef's third comment, I am trying to adapt the OLS Coefficients and Standard Errors Clustered by Firm and Year section of this example notebook below: cluster_2ways_ols = sm.ols(formula='y ~ x', data=df).fit(cov_type='cluster', cov_kwds={'groups': np.array(df[['firmid', 'year']])}, use_t=True) to my own example dataset. Note that I am able to reproduce this example (and it works). I can also add fixed-effects, by using 'y ~ x + C(firmid) + C(year)' as formula instead. Problem However, trying to port the same command to my example dataset (see code below), I'm getting the following error: >>> model = sm.OLS.from_formula("gdp ~ population + C(year_publication) + C(country)", df) >>> result = model.fit( cov_type='cluster', cov_kwds={'groups': np.array(df[['country', 'year_publication']])}, use_t=True ) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/path/venv/lib64/python3.10/site-packages/statsmodels/regression/linear_model.py", line 343, in fit lfit = OLSResults( File "/path/venv/lib64/python3.10/site-packages/statsmodels/regression/linear_model.py", line 1607, in __init__ self.get_robustcov_results(cov_type=cov_type, use_self=True, File "/path/venv/lib64/python3.10/site-packages/statsmodels/regression/linear_model.py", line 2568, in get_robustcov_results res.cov_params_default = sw.cov_cluster_2groups( File "/path/venv/lib64/python3.10/site-packages/statsmodels/stats/sandwich_covariance.py", line 591, in cov_cluster_2groups combine_indices(group)[0], File "/path/venv/lib64/python3.10/site-packages/statsmodels/tools/grouputils.py", line 55, in combine_indices groups_ = groups.view([('', groups.dtype)] * groups.shape[1]) File "/path/venv/lib64/python3.10/site-packages/numpy/core/_internal.py", line 549, in _view_is_safe raise TypeError("Cannot change data-type for object array.") TypeError: Cannot change data-type for object array. I have tried to manually cast the year_publication to string/object using np.array(df[['country', 'year_publication']].astype("str")), but it doesn't solve the issue. Questions What is the cause of the TypeError()? How to adapt the example command to my dataset? Minimal Working Example from io import StringIO import numpy as np import pandas as pd import statsmodels.api as sm DATA = """ "continent","country","source","year_publication","year_data","population","gdp" "Africa","Angola","OECD",2020,2018,972,52.69 "Africa","Angola","OECD",2020,2019,986,802.7 "Africa","Angola","OECD",2020,2020,641,568.74 "Africa","Angola","OECD",2021,2018,438,168.83 "Africa","Angola","OECD",2021,2019,958,310.57 "Africa","Angola","OECD",2021,2020,270,144.02 "Africa","Angola","OECD",2022,2018,528,359.71 "Africa","Angola","OECD",2022,2019,974,582.98 "Africa","Angola","OECD",2022,2020,835,820.49 "Africa","Angola","IMF",2020,2018,168,148.85 "Africa","Angola","IMF",2020,2019,460,236.21 "Africa","Angola","IMF",2020,2020,360,297.15 "Africa","Angola","IMF",2021,2018,381,249.13 "Africa","Angola","IMF",2021,2019,648,128.05 "Africa","Angola","IMF",2021,2020,206,179.05 "Africa","Angola","IMF",2022,2018,282,150.29 "Africa","Angola","IMF",2022,2019,125,23.42 "Africa","Angola","IMF",2022,2020,410,247.35 "Africa","Angola","WorldBank",2020,2018,553,182.06 "Africa","Angola","WorldBank",2020,2019,847,698.87 "Africa","Angola","WorldBank",2020,2020,844,126.61 "Africa","Angola","WorldBank",2021,2018,307,239.76 "Africa","Angola","WorldBank",2021,2019,659,510.73 "Africa","Angola","WorldBank",2021,2020,548,331.89 "Africa","Angola","WorldBank",2022,2018,448,122.76 "Africa","Angola","WorldBank",2022,2019,768,761.41 "Africa","Angola","WorldBank",2022,2020,324,163.57 "Africa","Benin","OECD",2020,2018,513,196.9 "Africa","Benin","OECD",2020,2019,590,83.7 "Africa","Benin","OECD",2020,2020,791,511.09 "Africa","Benin","OECD",2021,2018,799,474.43 "Africa","Benin","OECD",2021,2019,455,234.21 "Africa","Benin","OECD",2021,2020,549,238.83 "Africa","Benin","OECD",2022,2018,235,229.33 "Africa","Benin","OECD",2022,2019,347,46.51 "Africa","Benin","OECD",2022,2020,532,392.13 "Africa","Benin","IMF",2020,2018,138,137.05 "Africa","Benin","IMF",2020,2019,978,239.82 "Africa","Benin","IMF",2020,2020,821,33.41 "Africa","Benin","IMF",2021,2018,453,291.93 "Africa","Benin","IMF",2021,2019,526,381.88 "Africa","Benin","IMF",2021,2020,467,313.57 "Africa","Benin","IMF",2022,2018,948,555.23 "Africa","Benin","IMF",2022,2019,323,289.91 "Africa","Benin","IMF",2022,2020,421,62.35 "Africa","Benin","WorldBank",2020,2018,983,271.69 "Africa","Benin","WorldBank",2020,2019,138,23.55 "Africa","Benin","WorldBank",2020,2020,636,623.65 "Africa","Benin","WorldBank",2021,2018,653,534.99 "Africa","Benin","WorldBank",2021,2019,564,368.8 "Africa","Benin","WorldBank",2021,2020,741,312.02 "Africa","Benin","WorldBank",2022,2018,328,292.11 "Africa","Benin","WorldBank",2022,2019,653,429.21 "Africa","Benin","WorldBank",2022,2020,951,242.73 "Africa","Chad","OECD",2020,2018,176,95.06 "Africa","Chad","OECD",2020,2019,783,425.34 "Africa","Chad","OECD",2020,2020,885,461.6 "Africa","Chad","OECD",2021,2018,673,15.87 "Africa","Chad","OECD",2021,2019,131,74.46 "Africa","Chad","OECD",2021,2020,430,61.58 "Africa","Chad","OECD",2022,2018,593,211.34 "Africa","Chad","OECD",2022,2019,647,550.37 "Africa","Chad","OECD",2022,2020,154,105.65 "Africa","Chad","IMF",2020,2018,160,32.41 "Africa","Chad","IMF",2020,2019,654,27.84 "Africa","Chad","IMF",2020,2020,616,468.92 "Africa","Chad","IMF",2021,2018,996,22.4 "Africa","Chad","IMF",2021,2019,126,93.18 "Africa","Chad","IMF",2021,2020,879,547.87 "Africa","Chad","IMF",2022,2018,663,520 "Africa","Chad","IMF",2022,2019,681,544.76 "Africa","Chad","IMF",2022,2020,101,55.6 "Africa","Chad","WorldBank",2020,2018,786,757.22 "Africa","Chad","WorldBank",2020,2019,599,593.69 "Africa","Chad","WorldBank",2020,2020,641,529.84 "Africa","Chad","WorldBank",2021,2018,343,287.89 "Africa","Chad","WorldBank",2021,2019,438,340.83 "Africa","Chad","WorldBank",2021,2020,762,594.67 "Africa","Chad","WorldBank",2022,2018,430,128.69 "Africa","Chad","WorldBank",2022,2019,260,242.59 "Africa","Chad","WorldBank",2022,2020,607,216.1 "Europe","Denmark","OECD",2020,2018,114,86.75 "Europe","Denmark","OECD",2020,2019,937,373.29 "Europe","Denmark","OECD",2020,2020,866,392.93 "Europe","Denmark","OECD",2021,2018,296,41.04 "Europe","Denmark","OECD",2021,2019,402,32.67 "Europe","Denmark","OECD",2021,2020,306,7.88 "Europe","Denmark","OECD",2022,2018,540,379.51 "Europe","Denmark","OECD",2022,2019,108,26.72 "Europe","Denmark","OECD",2022,2020,752,307.2 "Europe","Denmark","IMF",2020,2018,157,24.24 "Europe","Denmark","IMF",2020,2019,303,79.04 "Europe","Denmark","IMF",2020,2020,286,122.36 "Europe","Denmark","IMF",2021,2018,569,69.32 "Europe","Denmark","IMF",2021,2019,808,642.67 "Europe","Denmark","IMF",2021,2020,157,5.58 "Europe","Denmark","IMF",2022,2018,147,112.21 "Europe","Denmark","IMF",2022,2019,414,311.16 "Europe","Denmark","IMF",2022,2020,774,230.46 "Europe","Denmark","WorldBank",2020,2018,695,350.03 "Europe","Denmark","WorldBank",2020,2019,511,209.84 "Europe","Denmark","WorldBank",2020,2020,181,29.27 "Europe","Denmark","WorldBank",2021,2018,503,176.89 "Europe","Denmark","WorldBank",2021,2019,710,609.02 "Europe","Denmark","WorldBank",2021,2020,264,165.78 "Europe","Denmark","WorldBank",2022,2018,670,638.99 "Europe","Denmark","WorldBank",2022,2019,651,354.6 "Europe","Denmark","WorldBank",2022,2020,632,623.94 "Europe","Estonia","OECD",2020,2018,838,263.67 "Europe","Estonia","OECD",2020,2019,638,533.95 "Europe","Estonia","OECD",2020,2020,898,638.73 "Europe","Estonia","OECD",2021,2018,262,98.16 "Europe","Estonia","OECD",2021,2019,569,552.54 "Europe","Estonia","OECD",2021,2020,868,252.48 "Europe","Estonia","OECD",2022,2018,927,264.65 "Europe","Estonia","OECD",2022,2019,205,150.6 "Europe","Estonia","OECD",2022,2020,828,752.61 "Europe","Estonia","IMF",2020,2018,841,176.31 "Europe","Estonia","IMF",2020,2019,614,230.55 "Europe","Estonia","IMF",2020,2020,500,41.19 "Europe","Estonia","IMF",2021,2018,510,169.68 "Europe","Estonia","IMF",2021,2019,765,401.85 "Europe","Estonia","IMF",2021,2020,751,319.6 "Europe","Estonia","IMF",2022,2018,314,58.81 "Europe","Estonia","IMF",2022,2019,155,2.24 "Europe","Estonia","IMF",2022,2020,734,187.6 "Europe","Estonia","WorldBank",2020,2018,332,160.17 "Europe","Estonia","WorldBank",2020,2019,466,385.33 "Europe","Estonia","WorldBank",2020,2020,487,435.06 "Europe","Estonia","WorldBank",2021,2018,461,249.19 "Europe","Estonia","WorldBank",2021,2019,932,763.38 "Europe","Estonia","WorldBank",2021,2020,650,463.91 "Europe","Estonia","WorldBank",2022,2018,570,549.97 "Europe","Estonia","WorldBank",2022,2019,909,80.48 "Europe","Estonia","WorldBank",2022,2020,523,242.22 "Europe","Finland","OECD",2020,2018,565,561.64 "Europe","Finland","OECD",2020,2019,646,161.62 "Europe","Finland","OECD",2020,2020,194,133.69 "Europe","Finland","OECD",2021,2018,529,39.76 "Europe","Finland","OECD",2021,2019,800,680.12 "Europe","Finland","OECD",2021,2020,418,399.19 "Europe","Finland","OECD",2022,2018,591,253.12 "Europe","Finland","OECD",2022,2019,457,272.58 "Europe","Finland","OECD",2022,2020,157,105.1 "Europe","Finland","IMF",2020,2018,860,445.03 "Europe","Finland","IMF",2020,2019,108,47.72 "Europe","Finland","IMF",2020,2020,523,500.58 "Europe","Finland","IMF",2021,2018,560,81.47 "Europe","Finland","IMF",2021,2019,830,664.64 "Europe","Finland","IMF",2021,2020,903,762.62 "Europe","Finland","IMF",2022,2018,179,167.73 "Europe","Finland","IMF",2022,2019,137,98.98 "Europe","Finland","IMF",2022,2020,666,524.86 "Europe","Finland","WorldBank",2020,2018,319,146.01 "Europe","Finland","WorldBank",2020,2019,401,219.56 "Europe","Finland","WorldBank",2020,2020,711,45.35 "Europe","Finland","WorldBank",2021,2018,828,20.97 "Europe","Finland","WorldBank",2021,2019,180,66.3 "Europe","Finland","WorldBank",2021,2020,682,92.57 "Europe","Finland","WorldBank",2022,2018,254,81.2 "Europe","Finland","WorldBank",2022,2019,619,159.08 "Europe","Finland","WorldBank",2022,2020,191,184.4 """ df = pd.read_csv(StringIO(DATA)) model = sm.OLS.from_formula("gdp ~ population + C(year_publication) + C(country)", df) result = model.fit( cov_type='cluster', cov_kwds={'groups': np.array(df[['country', 'year_publication']])}, use_t=True ) print(result.summary())
I have realized that the groups must be an array of integers rather than of objects/strings. Thus, label encoding the string column as follows: df["country"] = df["country"].astype("category") df["country_id"] = df.country.cat.codes and using country_id to cluster the standard errors solves the issue: result = model.fit( cov_type='cluster', cov_kwds={'groups': np.array(df[['country_id', 'year_publication']])}, use_t=True ) Fully working example: from io import StringIO import numpy as np import pandas as pd import statsmodels.api as sm DATA = """ "continent","country","source","year_publication","year_data","population","gdp" "Africa","Angola","OECD",2020,2018,972,52.69 "Africa","Angola","OECD",2020,2019,986,802.7 "Africa","Angola","OECD",2020,2020,641,568.74 "Africa","Angola","OECD",2021,2018,438,168.83 "Africa","Angola","OECD",2021,2019,958,310.57 "Africa","Angola","OECD",2021,2020,270,144.02 "Africa","Angola","OECD",2022,2018,528,359.71 "Africa","Angola","OECD",2022,2019,974,582.98 "Africa","Angola","OECD",2022,2020,835,820.49 "Africa","Angola","IMF",2020,2018,168,148.85 "Africa","Angola","IMF",2020,2019,460,236.21 "Africa","Angola","IMF",2020,2020,360,297.15 "Africa","Angola","IMF",2021,2018,381,249.13 "Africa","Angola","IMF",2021,2019,648,128.05 "Africa","Angola","IMF",2021,2020,206,179.05 "Africa","Angola","IMF",2022,2018,282,150.29 "Africa","Angola","IMF",2022,2019,125,23.42 "Africa","Angola","IMF",2022,2020,410,247.35 "Africa","Angola","WorldBank",2020,2018,553,182.06 "Africa","Angola","WorldBank",2020,2019,847,698.87 "Africa","Angola","WorldBank",2020,2020,844,126.61 "Africa","Angola","WorldBank",2021,2018,307,239.76 "Africa","Angola","WorldBank",2021,2019,659,510.73 "Africa","Angola","WorldBank",2021,2020,548,331.89 "Africa","Angola","WorldBank",2022,2018,448,122.76 "Africa","Angola","WorldBank",2022,2019,768,761.41 "Africa","Angola","WorldBank",2022,2020,324,163.57 "Africa","Benin","OECD",2020,2018,513,196.9 "Africa","Benin","OECD",2020,2019,590,83.7 "Africa","Benin","OECD",2020,2020,791,511.09 "Africa","Benin","OECD",2021,2018,799,474.43 "Africa","Benin","OECD",2021,2019,455,234.21 "Africa","Benin","OECD",2021,2020,549,238.83 "Africa","Benin","OECD",2022,2018,235,229.33 "Africa","Benin","OECD",2022,2019,347,46.51 "Africa","Benin","OECD",2022,2020,532,392.13 "Africa","Benin","IMF",2020,2018,138,137.05 "Africa","Benin","IMF",2020,2019,978,239.82 "Africa","Benin","IMF",2020,2020,821,33.41 "Africa","Benin","IMF",2021,2018,453,291.93 "Africa","Benin","IMF",2021,2019,526,381.88 "Africa","Benin","IMF",2021,2020,467,313.57 "Africa","Benin","IMF",2022,2018,948,555.23 "Africa","Benin","IMF",2022,2019,323,289.91 "Africa","Benin","IMF",2022,2020,421,62.35 "Africa","Benin","WorldBank",2020,2018,983,271.69 "Africa","Benin","WorldBank",2020,2019,138,23.55 "Africa","Benin","WorldBank",2020,2020,636,623.65 "Africa","Benin","WorldBank",2021,2018,653,534.99 "Africa","Benin","WorldBank",2021,2019,564,368.8 "Africa","Benin","WorldBank",2021,2020,741,312.02 "Africa","Benin","WorldBank",2022,2018,328,292.11 "Africa","Benin","WorldBank",2022,2019,653,429.21 "Africa","Benin","WorldBank",2022,2020,951,242.73 "Africa","Chad","OECD",2020,2018,176,95.06 "Africa","Chad","OECD",2020,2019,783,425.34 "Africa","Chad","OECD",2020,2020,885,461.6 "Africa","Chad","OECD",2021,2018,673,15.87 "Africa","Chad","OECD",2021,2019,131,74.46 "Africa","Chad","OECD",2021,2020,430,61.58 "Africa","Chad","OECD",2022,2018,593,211.34 "Africa","Chad","OECD",2022,2019,647,550.37 "Africa","Chad","OECD",2022,2020,154,105.65 "Africa","Chad","IMF",2020,2018,160,32.41 "Africa","Chad","IMF",2020,2019,654,27.84 "Africa","Chad","IMF",2020,2020,616,468.92 "Africa","Chad","IMF",2021,2018,996,22.4 "Africa","Chad","IMF",2021,2019,126,93.18 "Africa","Chad","IMF",2021,2020,879,547.87 "Africa","Chad","IMF",2022,2018,663,520 "Africa","Chad","IMF",2022,2019,681,544.76 "Africa","Chad","IMF",2022,2020,101,55.6 "Africa","Chad","WorldBank",2020,2018,786,757.22 "Africa","Chad","WorldBank",2020,2019,599,593.69 "Africa","Chad","WorldBank",2020,2020,641,529.84 "Africa","Chad","WorldBank",2021,2018,343,287.89 "Africa","Chad","WorldBank",2021,2019,438,340.83 "Africa","Chad","WorldBank",2021,2020,762,594.67 "Africa","Chad","WorldBank",2022,2018,430,128.69 "Africa","Chad","WorldBank",2022,2019,260,242.59 "Africa","Chad","WorldBank",2022,2020,607,216.1 "Europe","Denmark","OECD",2020,2018,114,86.75 "Europe","Denmark","OECD",2020,2019,937,373.29 "Europe","Denmark","OECD",2020,2020,866,392.93 "Europe","Denmark","OECD",2021,2018,296,41.04 "Europe","Denmark","OECD",2021,2019,402,32.67 "Europe","Denmark","OECD",2021,2020,306,7.88 "Europe","Denmark","OECD",2022,2018,540,379.51 "Europe","Denmark","OECD",2022,2019,108,26.72 "Europe","Denmark","OECD",2022,2020,752,307.2 "Europe","Denmark","IMF",2020,2018,157,24.24 "Europe","Denmark","IMF",2020,2019,303,79.04 "Europe","Denmark","IMF",2020,2020,286,122.36 "Europe","Denmark","IMF",2021,2018,569,69.32 "Europe","Denmark","IMF",2021,2019,808,642.67 "Europe","Denmark","IMF",2021,2020,157,5.58 "Europe","Denmark","IMF",2022,2018,147,112.21 "Europe","Denmark","IMF",2022,2019,414,311.16 "Europe","Denmark","IMF",2022,2020,774,230.46 "Europe","Denmark","WorldBank",2020,2018,695,350.03 "Europe","Denmark","WorldBank",2020,2019,511,209.84 "Europe","Denmark","WorldBank",2020,2020,181,29.27 "Europe","Denmark","WorldBank",2021,2018,503,176.89 "Europe","Denmark","WorldBank",2021,2019,710,609.02 "Europe","Denmark","WorldBank",2021,2020,264,165.78 "Europe","Denmark","WorldBank",2022,2018,670,638.99 "Europe","Denmark","WorldBank",2022,2019,651,354.6 "Europe","Denmark","WorldBank",2022,2020,632,623.94 "Europe","Estonia","OECD",2020,2018,838,263.67 "Europe","Estonia","OECD",2020,2019,638,533.95 "Europe","Estonia","OECD",2020,2020,898,638.73 "Europe","Estonia","OECD",2021,2018,262,98.16 "Europe","Estonia","OECD",2021,2019,569,552.54 "Europe","Estonia","OECD",2021,2020,868,252.48 "Europe","Estonia","OECD",2022,2018,927,264.65 "Europe","Estonia","OECD",2022,2019,205,150.6 "Europe","Estonia","OECD",2022,2020,828,752.61 "Europe","Estonia","IMF",2020,2018,841,176.31 "Europe","Estonia","IMF",2020,2019,614,230.55 "Europe","Estonia","IMF",2020,2020,500,41.19 "Europe","Estonia","IMF",2021,2018,510,169.68 "Europe","Estonia","IMF",2021,2019,765,401.85 "Europe","Estonia","IMF",2021,2020,751,319.6 "Europe","Estonia","IMF",2022,2018,314,58.81 "Europe","Estonia","IMF",2022,2019,155,2.24 "Europe","Estonia","IMF",2022,2020,734,187.6 "Europe","Estonia","WorldBank",2020,2018,332,160.17 "Europe","Estonia","WorldBank",2020,2019,466,385.33 "Europe","Estonia","WorldBank",2020,2020,487,435.06 "Europe","Estonia","WorldBank",2021,2018,461,249.19 "Europe","Estonia","WorldBank",2021,2019,932,763.38 "Europe","Estonia","WorldBank",2021,2020,650,463.91 "Europe","Estonia","WorldBank",2022,2018,570,549.97 "Europe","Estonia","WorldBank",2022,2019,909,80.48 "Europe","Estonia","WorldBank",2022,2020,523,242.22 "Europe","Finland","OECD",2020,2018,565,561.64 "Europe","Finland","OECD",2020,2019,646,161.62 "Europe","Finland","OECD",2020,2020,194,133.69 "Europe","Finland","OECD",2021,2018,529,39.76 "Europe","Finland","OECD",2021,2019,800,680.12 "Europe","Finland","OECD",2021,2020,418,399.19 "Europe","Finland","OECD",2022,2018,591,253.12 "Europe","Finland","OECD",2022,2019,457,272.58 "Europe","Finland","OECD",2022,2020,157,105.1 "Europe","Finland","IMF",2020,2018,860,445.03 "Europe","Finland","IMF",2020,2019,108,47.72 "Europe","Finland","IMF",2020,2020,523,500.58 "Europe","Finland","IMF",2021,2018,560,81.47 "Europe","Finland","IMF",2021,2019,830,664.64 "Europe","Finland","IMF",2021,2020,903,762.62 "Europe","Finland","IMF",2022,2018,179,167.73 "Europe","Finland","IMF",2022,2019,137,98.98 "Europe","Finland","IMF",2022,2020,666,524.86 "Europe","Finland","WorldBank",2020,2018,319,146.01 "Europe","Finland","WorldBank",2020,2019,401,219.56 "Europe","Finland","WorldBank",2020,2020,711,45.35 "Europe","Finland","WorldBank",2021,2018,828,20.97 "Europe","Finland","WorldBank",2021,2019,180,66.3 "Europe","Finland","WorldBank",2021,2020,682,92.57 "Europe","Finland","WorldBank",2022,2018,254,81.2 "Europe","Finland","WorldBank",2022,2019,619,159.08 "Europe","Finland","WorldBank",2022,2020,191,184.4 """ df = pd.read_csv(StringIO(DATA)) df["country"] = df["country"].astype("category") df["country_id"] = df.country.cat.codes model = sm.OLS.from_formula("gdp ~ population + C(year_publication) + C(country)", df) result = model.fit( cov_type='cluster', cov_kwds={'groups': np.array(df[['country_id', 'year_publication']])}, use_t=True ) print(result.summary())
Plot wont display in python
I'm using the following bit of code to plot two arrays of the same length - import matplotlib.pyplot as plt from scipy import stats from scipy.stats import linregress G_mag_values = [11.436, 11.341, 11.822, 11.646, 11.924, 12.057, 11.884, 11.805, 12.347, 12.662, 12.362, 12.555, 12.794, 12.819, 12.945, 12.733, 12.789, 12.878, 12.963, 13.094, 13.031, 12.962, 13.018, 12.906, 13.016, 13.088, 13.04, 13.035, 13.094, 13.032, 13.216, 13.062, 13.083, 13.126, 13.101, 13.089, 13.073, 13.182, 13.116, 13.145, 13.235, 13.161, 13.154, 13.383, 13.315, 13.429, 13.461, 13.287, 13.494, 13.459, 13.478, 13.534, 13.536, 13.536, 13.483, 13.544, 13.564, 13.544, 13.608, 13.655, 13.665, 13.668, 13.697, 13.649, 13.742, 13.756, 13.671, 13.701, 13.788, 13.723, 13.697, 13.713, 13.708, 13.765, 13.847, 13.992, 13.706, 13.79, 13.783, 13.844, 13.945, 13.928, 13.936, 13.956, 13.898, 14.059, 13.945, 14.039, 13.999, 14.087, 14.05, 14.083, 14.136, 14.124, 14.189, 14.149, 14.182, 14.281, 14.177, 14.297, 14.268, 14.454, 14.295] G_cal_values = [-8.553610547563503, -8.085853602272588, -7.98491731861732, -7.852060056526794, -7.550944423333883, -7.569289687795749, -7.547088847268468, -7.544445036682168, -7.480698829329534, -7.184407435874912, -7.382606680295108, -7.2231275160942054, -7.093385973539046, -7.0473097125206685, -6.775012624594927, -6.814667514017907, -6.719898703328146, -6.741699011193633, -6.483121454948265, -6.320533066162438, -6.216044707275117, -6.037365656714626, -6.058593802250578, -6.0203190345840865, -6.036176430437363, -5.817887798572345, -5.838439347527171, -5.864922270102037, -5.755152671040021, -5.7709095683554725, -5.729226240967218, -5.606533007538604, -5.5817719334376905, -5.578993138005095, -5.62616747769538, -5.648413591916503, -5.611676700504294, -5.557722166623976, -5.5584623064502825, -5.425878164810264, -5.582204334985258, -5.529395790688368, -5.560750195967957, -5.433224654816512, -5.4751198268734385, -5.4592032005417215, -5.514591770369543, -5.580278698184566, -5.520695348050357, -5.501615700174841, -5.578645415877418, -5.692203332547151, -5.569497861450115, -5.335209902666812, -5.470963349023013, -5.44265375533589, -5.538541653702721, -5.355732832969871, -5.318164588926453, -5.376154615199398, -5.372133774215322, -5.361689907587619, -5.37608154921679, -5.412657572197508, -5.454613589602333, -5.339384591430104, -5.367511403407703, -5.258069473329993, -5.347580031901464, -4.9905279263992, -5.445096880253789, -5.192885553786512, -5.2983352094538505, -5.3930571447307365, -5.057910469318639, -5.32585105504838, -5.238649399637653, -5.122431894813153, -5.084559296025157, -5.139042420486851, -4.9919273140342915, -5.103619454431522, -5.017946144298159, -4.98136832081144, -5.084355565584671, -5.048634391386494, -4.887073481359435, -5.074683293771264, -5.050703776716202, -5.104772289705188, -4.9597601680524415, -4.971489935988787, -4.895283369236485, -4.9859511256778974, -4.840717539517584, -4.815665714699117, -4.937635861879118, -4.887219819695687, -4.813729758415283, -4.82667464608015, -4.865176481168528, -4.885105289124561, -4.887072278243732] fig, ax = plt.subplots() plt.scatter(G_mag_values,G_cal_values) ax.minorticks_on() ax.grid(which='major', linestyle='-', linewidth='0.5') ax.grid(which='minor', linestyle='-', linewidth='0.5') fig.set_size_inches(10,7) best_fit_Y_G = [] slope_G, intercept_G, r_value_G, p_value_G, std_err_G = stats.linregress(G_mag_values,G_cal_values) for value_G in G_mag_values: best_fit_Y_G.append(intercept_G + slope_G*value_G) plt.plot(G_mag_values, best_fit_Y_G, 'r', label = 'Best fit') plt.title('M67 Calibration graph for G filter') plt.xlabel('Real magnitude') plt.ylabel('Measured magnitude') plt.show() curve_G = np.polyfit(G_mag_values,G_cal_values,1) print('G filter polyfit line: slope {}; intercept = {}'.format(curve_G[0],curve_G[1])) print('G filter linregress: slope {}; intercept = {}'.format(slope_G,intercept_G)) When I run this, it prints the values for slope and intercept from the best_fit_Y_G and curve_G, but it doesnt display the plot at all. Where am I going wrong?
I copy/pasted and run your code. curve_G = np.polyfit(G_mag_values,G_cal_values,1) That line gives me error. Then I imported numpy as np and problem solved. output figure
Python - Fit gaussian to noisy data with lmfit
I'm trying to fit a gaussian to this data x = [4170.177259096838, 4170.377258006199, 4170.577256915561, 4170.777255824922, 4170.977254734283, 4171.177253643645, 4171.377252553006, 4171.577251462368, 4171.777250371729, 4171.977249281091, 4172.177248190453, 4172.377247099814, 4172.577246009175, 4172.777244918537, 4172.977243827898, 4173.17724273726, 4173.377241646621, 4173.577240555983, 4173.777239465344, 4173.977238374706, 4174.177237284067, 4174.377236193429, 4174.57723510279, 4174.777234012152, 4174.977232921513, 4175.177231830875, 4175.377230740236, 4175.577229649598, 4175.777228558959, 4175.977227468321, 4176.177226377682, 4176.377225287044, 4176.577224196405, 4176.777223105767, 4176.977222015128, 4177.17722092449, 4177.377219833851, 4177.577218743213, 4177.777217652574, 4177.977216561936, 4178.177215471297, 4178.377214380659, 4178.57721329002, 4178.777212199382, 4178.977211108743, 4179.177210018105, 4179.377208927466, 4179.577207836828, 4179.777206746189, 4179.977205655551, 4180.177204564912, 4180.377203474274, 4180.577202383635, 4180.777201292997, 4180.977200202357, 4181.17719911172, 4181.377198021081, 4181.577196930443, 4181.777195839804, 4181.977194749166, 4182.177193658527, 4182.377192567888, 4182.5771914772495, 4182.777190386612, 4182.9771892959725, 4183.177188205335, 4183.377187114696, 4183.577186024058, 4183.777184933419, 4183.9771838427805, 4184.177182752143, 4184.3771816615035, 4184.5771805708655, 4184.777179480228, 4184.977178389589, 4185.1771772989505, 4185.3771762083115, 4185.5771751176735, 4185.777174027035, 4185.977172936397, 4186.1771718457585, 4186.3771707551205, 4186.5771696644815, 4186.777168573843, 4186.977167483204, 4187.177166392566, 4187.377165301927, 4187.577164211289, 4187.77716312065, 4187.977162030013, 4188.177160939374, 4188.377159848735, 4188.577158758096, 4188.777157667458, 4188.977156576819, 4189.177155486181, 4189.377154395542, 4189.577153304904, 4189.777152214265, 4189.977151123627, 4190.177150032989, 4190.37714894235, 4190.577147851711, 4190.777146761073, 4190.977145670434, 4191.177144579796, 4191.377143489157, 4191.577142398519, 4191.77714130788, 4191.977140217242, 4192.177139126603, 4192.377138035965, 4192.577136945326, 4192.777135854688, 4192.977134764049, 4193.177133673411, 4193.377132582772, 4193.577131492134, 4193.777130401495, 4193.977129310857, 4194.177128220218, 4194.377127129579, 4194.577126038941, 4194.777124948303, 4194.977123857664, 4195.177122767026, 4195.377121676387, 4195.577120585749, 4195.77711949511, 4195.977118404472, 4196.177117313833, 4196.377116223195, 4196.577115132556, 4196.777114041918, 4196.977112951279, 4197.177111860641, 4197.377110770002, 4197.577109679364, 4197.777108588725, 4197.977107498087, 4198.177106407448, 4198.37710531681, 4198.577104226171, 4198.777103135533, 4198.977102044893, 4199.177100954256, 4199.377099863617, 4199.577098772979, 4199.77709768234, 4199.977096591702, 4200.177095501063, 4200.377094410424, 4200.5770933197855, 4200.777092229148, 4200.9770911385085, 4201.177090047871, 4201.377088957232, 4201.577087866594, 4201.7770867759555, 4201.9770856853165, 4202.177084594679, 4202.377083504041, 4202.5770824134015, 4202.777081322764, 4202.977080232125, 4203.1770791414865, 4203.377078050848, 4203.5770769602095, 4203.777075869571, 4203.9770747789335, 4204.1770736882945, 4204.3770725976565, 4204.5770715070175, 4204.777070416379, 4204.97706932574, 4205.177068235102, 4205.377067144463, 4205.577066053825, 4205.777064963186, 4205.977063872549, 4206.17706278191, 4206.377061691271, 4206.577060600632, 4206.777059509994, 4206.977058419355, 4207.177057328717, 4207.377056238078, 4207.57705514744, 4207.777054056801, 4207.977052966163, 4208.177051875525, 4208.377050784886, 4208.577049694247, 4208.777048603609, 4208.977047512971, 4209.177046422332, 4209.377045331693, 4209.577044241055, 4209.777043150416, 4209.977042059778, 4210.177040969139, 4210.377039878501, 4210.577038787862, 4210.777037697224, 4210.977036606585, 4211.177035515947, 4211.377034425308, 4211.57703333467, 4211.777032244031, 4211.977031153393, 4212.177030062754, 4212.377028972116, 4212.577027881477, 4212.777026790839, 4212.9770257002, 4213.177024609562, 4213.377023518923, 4213.577022428285, 4213.777021337646, 4213.977020247008, 4214.177019156369, 4214.377018065731, 4214.577016975092, 4214.777015884454, 4214.977014793814, 4215.177013703177, 4215.377012612538, 4215.5770115219, 4215.777010431261, 4215.977009340623, 4216.177008249984, 4216.377007159345, 4216.577006068707, 4216.777004978069, 4216.977003887429, 4217.177002796792, 4217.377001706153, 4217.577000615515, 4217.776999524876, 4217.976998434238, 4218.176997343599, 4218.37699625296, 4218.5769951623215, 4218.776994071684, 4218.9769929810445, 4219.176991890407, 4219.376990799769, 4219.5769897091295, 4219.7769886184915, 4219.9769875278525, 4220.176986437215, 4220.376985346577, 4220.5769842559375, 4220.7769831653, 4220.9769820746615, 4221.1769809840225, 4221.376979893384, 4221.5769788027455, 4221.776977712107, 4221.9769766214695, 4222.17697553083, 4222.3769744401925, 4222.576973349554, 4222.776972258915, 4222.976971168276, 4223.176970077638, 4223.376968986999, 4223.576967896361, 4223.776966805722, 4223.976965715085, 4224.176964624445, 4224.376963533807, 4224.576962443168, 4224.77696135253, 4224.976960261891, 4225.176959171253, 4225.376958080614, 4225.576956989976, 4225.776955899337, 4225.976954808699, 4226.17695371806, 4226.376952627422, 4226.576951536783, 4226.776950446145, 4226.976949355506, 4227.176948264868, 4227.376947174229, 4227.576946083591, 4227.776944992952, 4227.976943902314, 4228.176942811675, 4228.376941721037, 4228.576940630398, 4228.776939539759, 4228.976938449121, 4229.176937358483, 4229.376936267844, 4229.576935177205, 4229.776934086567, 4229.976932995929] y = [1.0063203573226929, 0.9789621233940125, 0.9998905658721924, 0.9947001934051514, 1.023498773574829, 1.0001505613327026, 0.9659610986709596, 1.0141736268997192, 0.9910064339637756, 0.961456060409546, 0.9808377623558044, 0.9717124700546264, 1.0020164251327517, 0.9276596307754515, 1.0044682025909424, 0.9898168444633484, 1.0139398574829102, 1.016809344291687, 0.9985541105270386, 1.0404949188232422, 1.0104306936264038, 1.0101377964019775, 1.0228283405303955, 1.014385461807251, 0.9949180483818054, 0.9398794174194336, 1.0047662258148191, 1.0185784101486206, 0.9942153096199036, 1.0496678352355957, 0.929694890975952, 1.0259612798690796, 1.0174839496612549, 0.9557819366455078, 1.009858012199402, 1.0258405208587646, 1.0318727493286133, 0.9781686067581176, 0.9566296339035034, 0.9626089930534364, 1.040783166885376, 0.9469046592712402, 0.9732370972633362, 1.0082777738571167, 1.0438332557678225, 1.067220687866211, 1.0809389352798462, 1.0122139453887942, 0.995375156402588, 1.025692343711853, 1.0900095701217651, 1.0033329725265503, 0.9947514533996582, 0.9366152882575988, 1.0340673923492432, 1.0574461221694946, 0.9984419345855712, 0.9406535029411316, 1.0367794036865234, 1.0252420902252195, 0.9390246868133544, 1.057265043258667, 1.0652446746826172, 1.0001699924468994, 1.0561981201171875, 0.9452269077301024, 1.0119216442108154, 1.000349760055542, 0.9879921674728394, 0.9834288954734802, 0.976799249649048, 0.9408118724822998, 1.0574927330017092, 1.0466219186782837, 0.97526878118515, 0.9811903238296508, 0.9985196590423584, 0.9862677454948424, 0.964194357395172, 1.0116554498672483, 0.9122620820999146, 0.9972245693206788, 0.9447768926620485, 1.0320085287094116, 1.0034307241439822, 0.965615689754486, 1.0228805541992188, 0.9555847048759459, 1.00389301776886, 0.9856386780738832, 0.9894683361053468, 1.0711736679077148, 0.990192711353302, 1.016653060913086, 1.0263935327529907, 0.9454292058944702, 0.9236765503883362, 0.9511216878890992, 0.9773555994033812, 0.9222095608711244, 0.9599731564521791, 1.0067923069000244, 1.0022263526916504, 0.9766445159912108, 1.0026237964630127, 1.010635256767273, 0.9901092052459716, 0.9869268536567688, 1.0354781150817869, 0.9797658920288086, 0.9543874263763428, 0.9747632145881652, 0.9942164421081544, 1.008299469947815, 0.9546594023704528, 1.0318409204483032, 1.0383642911911009, 1.0332415103912354, 1.0234425067901611, 1.0186198949813845, 1.0179851055145264, 1.0760197639465332, 0.9456835985183716, 1.0079874992370603, 0.9838529229164124, 0.8951097726821899, 0.9530791640281676, 0.9732348322868348, 0.9659185409545898, 1.0089071989059448, 0.963958203792572, 1.0035384893417358, 0.9776629805564879, 0.964256465435028, 0.9468261599540709, 1.0145124197006226, 1.0375784635543823, 0.992344319820404, 0.9584225416183472, 1.0427420139312744, 0.9997742176055908, 0.9584409594535828, 1.0051720142364502, 0.9606672525405884, 0.9797580242156982, 0.9900978207588196, 0.943138301372528, 0.9368865489959716, 0.9272330403327942, 0.9655094146728516, 0.9074565172195436, 0.97406405210495, 0.8742623329162598, 0.9219859838485718, 0.9126378297805786, 0.8354664444923401, 0.9138413667678832, 0.9268960952758788, 0.8841327428817749, 0.9733222126960754, 0.8825243711471558, 0.9243521094322203, 0.9403685927391052, 0.8782523870468141, 0.9003781080245972, 0.8850597143173218, 0.9231640696525574, 0.931676983833313, 0.8601804971694946, 0.8312444686889648, 0.9361259937286376, 0.9289224147796632, 0.8919285535812378, 0.8838070034980774, 0.9187015891075134, 0.9484543204307556, 0.8572731018066406, 0.8458079099655151, 0.92625629901886, 0.9748064875602722, 0.9674397706985474, 0.9326313138008118, 0.9933922290802002, 1.0025516748428345, 0.9956294894218444, 0.8995802998542786, 0.9598655700683594, 1.0185420513153076, 0.9935647249221802, 0.9689980745315552, 0.9919951558113098, 1.0028616189956665, 1.0252325534820557, 1.0221387147903442, 1.009528875350952, 1.0272767543792725, 0.9865442514419556, 0.9821861386299132, 0.95982563495636, 0.9557262063026428, 0.9864148497581482, 1.0166704654693604, 1.0599093437194824, 1.0000406503677368, 0.9622656106948853, 1.0044697523117063, 1.0404677391052246, 1.0023702383041382, 0.9803014993667604, 1.0197279453277588, 0.9902933835983276, 0.998839259147644, 0.966608464717865, 1.0340296030044556, 0.9632315635681152, 0.9758646488189696, 0.9757773876190186, 0.9818265438079834, 1.0110433101654053, 1.0131133794784546, 1.0256367921829224, 1.0690158605575562, 0.9764784574508668, 0.9947471022605896, 0.9979920387268066, 0.9850373864173888, 0.9165602922439576, 0.9634824395179749, 1.052489995956421, 0.9370544552803041, 1.0348092317581177, 1.0473220348358154, 0.9566289782524108, 0.9579214453697203, 0.972671627998352, 0.9536439180374146, 0.9755330085754396, 0.9753606915473938, 0.9924075603485109, 0.9893715381622314, 0.9780346751213074, 1.0207450389862058, 0.9914312362670898, 0.9940584301948548, 1.0417673587799072, 0.977041721343994, 1.0113568305969238, 1.030456304550171, 1.0540854930877686, 0.9963837265968324, 1.002269268035889, 0.9528346061706544, 0.9132148027420044, 1.0386162996292114, 0.9384365677833556, 1.0175614356994631, 1.0362330675125122, 0.9502999186515808, 1.0015273094177246, 0.987025022506714, 0.9869014024734496, 0.9577396512031556, 0.9633736610412598, 1.0747206211090088, 1.1858476400375366, 0.9917531609535216, 1.0963184833526611, 0.9528627991676332, 0.9999563694000244, 1.0115929841995241, 1.0094747543334959, 0.9977090358734132, 0.9800350666046144, 1.0336441993713381, 1.0021690130233765, 0.9629588127136229, 0.9191407561302184, 0.9930744767189026, 1.0318671464920044, 0.975939691066742, 0.9548277258872986, 1.0113637447357178, 0.9920935630798341, 0.9777255654335022, 0.9780721664428712, 0.9507009387016296, 0.9387223720550536, 1.0220414400100708, 1.019809007644653, 0.9822806715965272, 1.0380866527557373, 1.0477066040039062, 1.0222935676574707, 1.0258997678756714, 1.027082443237305, 1.0487046241760254, 0.9292799830436708, 0.999277114868164, 1.044923186302185, 1.0261610746383667] e = [3.865531107294373e-05, 3.866014958475717e-05, 3.866496626869776e-05, 3.8669764762744314e-05, 3.867453415296041e-05, 3.8679270801367245e-05, 3.8683978345943615e-05, 3.868864223477431e-05, 3.8693269743816934e-05, 3.8697849959135056e-05, 3.870237924274989e-05, 3.8706857594661415e-05, 3.871127773891203e-05, 3.871564331348054e-05, 3.871994340443053e-05, 3.872417437378317e-05, 3.8728336221538484e-05, 3.8732425309717655e-05, 3.8736438000341884e-05, 3.874037065543234e-05, 3.8744219637010247e-05, 3.874798130709678e-05, 3.8751652027713135e-05, 3.875523543683812e-05, 3.8758716982556514e-05, 3.876210394082591e-05, 3.8765389035688706e-05, 3.8768568629166105e-05, 3.87716390832793e-05, 3.877460039802827e-05, 3.877745257341303e-05, 3.878018469549716e-05, 3.8782800402259454e-05, 3.878529605572112e-05, 3.8787664379924536e-05, 3.878991265082732e-05, 3.8792029954493046e-05, 3.8794016290921725e-05, 3.879586802213453e-05, 3.8797588786110275e-05, 3.879916766891256e-05, 3.8800608308520175e-05, 3.88019070669543e-05, 3.880306030623615e-05, 3.880407166434452e-05, 3.8804930227342986e-05, 3.8805643271189176e-05, 3.880619988194667e-05, 3.880660733557306e-05, 3.8806854718131945e-05, 3.8806945667602115e-05, 3.88068801839836e-05, 3.880665099131875e-05, 3.8806265365565196e-05, 3.880571239278652e-05, 3.880499571096152e-05, 3.880410804413259e-05, 3.880305666825734e-05, 3.8801834307378165e-05, 3.8800444599473856e-05, 3.87988802685868e-05, 3.879714495269582e-05, 3.8795235013822094e-05, 3.879315045196563e-05, 3.879089126712642e-05, 3.8788453821325675e-05, 3.8785838114563376e-05, 3.878304414683953e-05, 3.8780071918154135e-05, 3.877691779052839e-05, 3.877357812598348e-05, 3.877006747643463e-05, 3.8766367651987814e-05, 3.876248956657946e-05, 3.875842594425194e-05, 3.8754180422984064e-05, 3.8749749364797026e-05, 3.874513640766963e-05, 3.8740334275644266e-05, 3.873535024467856e-05, 3.8730184314772493e-05, 3.872482920996845e-05, 3.871929220622405e-05, 3.871356602758169e-05, 3.8707657949998975e-05, 3.8701564335497096e-05, 3.8695285184076056e-05, 3.868882413371466e-05, 3.86821739084553e-05, 3.867534178425558e-05, 3.86683241231367e-05, 3.8661124563077465e-05, 3.8653739466099075e-05, 3.8646172470180325e-05, 3.863842357532121e-05, 3.863049278152175e-05, 3.862238008878194e-05, 3.861408913508057e-05, 3.860561628243886e-05, 3.85969651688356e-05, 3.8588135794270784e-05, 3.8579128158744425e-05, 3.856993862427771e-05, 3.856058174278587e-05, 3.855104296235368e-05, 3.854133319691755e-05, 3.853144880849868e-05, 3.852139343507588e-05, 3.851116707664913e-05, 3.8500766095239676e-05, 3.8490205042762675e-05, 3.847947300528176e-05, 3.846857362077572e-05, 3.8457506889244535e-05, 3.844628372462465e-05, 3.843489321297966e-05, 3.8423342630267136e-05, 3.841163197648712e-05, 3.8399768527597196e-05, 3.8387741369660944e-05, 3.8375561416614794e-05, 3.836322866845876e-05, 3.835074676317163e-05, 3.8338112062774605e-05, 3.832533184322529e-05, 3.831240246654488e-05, 3.829932757071219e-05, 3.828611079370603e-05, 3.827275213552639e-05, 3.825925523415208e-05, 3.8245623727561906e-05, 3.8231850339798264e-05, 3.821794962277636e-05, 3.820391066255979e-05, 3.818974801106378e-05, 3.817545439233072e-05, 3.816103708231821e-05, 3.814649244304746e-05, 3.8131831388454884e-05, 3.811704664258287e-05, 3.810214911936782e-05, 3.8087135180830956e-05, 3.807200482697226e-05, 3.805676897172816e-05, 3.804142033914104e-05, 3.802596984314732e-05, 3.801041020778939e-05, 3.799475598498248e-05, 3.7978999898768955e-05, 3.7963145587127656e-05, 3.794720396399498e-05, 3.793116411543451e-05, 3.791503331740387e-05, 3.789882612181827e-05, 3.78825279767625e-05, 3.786614615819417e-05, 3.784968066611327e-05, 3.783314969041385e-05, 3.781653504120186e-05, 3.7799851270392544e-05, 3.7783102015964694e-05, 3.776627636398189e-05, 3.774939614231698e-05, 3.773245043703355e-05, 3.77154428861104e-05, 3.769838440348394e-05, 3.7681271351175376e-05, 3.7664103729184724e-05, 3.764688881346956e-05, 3.762963024200872e-05, 3.7612328014802194e-05, 3.759498213184997e-05, 3.7577603507088504e-05, 3.756018850253895e-05, 3.754274075618014e-05, 3.752526026801206e-05, 3.7507754313992336e-05, 3.749022653209977e-05, 3.747267328435555e-05, 3.7455109122674905e-05, 3.7437519495142624e-05, 3.741991895367392e-05, 3.740230749826878e-05, 3.7384688766906045e-05, 3.736707003554329e-05, 3.7349444028222933e-05, 3.733181438292377e-05, 3.731419565156102e-05, 3.729656964424066e-05, 3.727895818883553e-05, 3.726136128534563e-05, 3.724377893377096e-05, 3.72262074961327e-05, 3.7208654248388484e-05, 3.719112282851711e-05, 3.717361323651858e-05, 3.71561327483505e-05, 3.713868500199169e-05, 3.712127363542095e-05, 3.710389137268066e-05, 3.708654185174965e-05, 3.7069235986564315e-05, 3.7051977415103465e-05, 3.70347588614095e-05, 3.7017580325482406e-05, 3.7000467273173854e-05, 3.698339060065337e-05, 3.6966375773772604e-05, 3.694941915455274e-05, 3.6932531656930216e-05, 3.69156914530322e-05, 3.68989203707315e-05, 3.688222204800695e-05, 3.686558557092212e-05, 3.684902549139224e-05, 3.68325381714385e-05, 3.681613088701852e-05, 3.679980000015348e-05, 3.67835491488222e-05, 3.6767385608982295e-05, 3.675130210467614e-05, 3.6735313187818974e-05, 3.6719411582453176e-05, 3.670359728857875e-05, 3.668788122013211e-05, 3.6672267015092075e-05, 3.6656743759522215e-05, 3.6641329643316574e-05, 3.6626006476581103e-05, 3.661079972516745e-05, 3.65956875612028e-05, 3.658069908851757e-05, 3.656581247923896e-05, 3.655103500932455e-05, 3.653637395473197e-05, 3.652183659141883e-05, 3.650740836746991e-05, 3.649310383480042e-05, 3.647892663138919e-05, 3.6464858567342155e-05, 3.645092147053219e-05, 3.6437118978938095e-05, 3.642343290266581e-05, 3.64098850695882e-05, 3.6396453651832423e-05, 3.638317502918653e-05, 3.637001282186248e-05, 3.635699613369071e-05, 3.6344117688713595e-05, 3.633138112490997e-05, 3.631877552834339e-05, 3.6306315450929105e-05, 3.62940008926671e-05, 3.628181730164215e-05, 3.626977922976948e-05, 3.6257904866943136e-05, 3.6246172385290265e-05, 3.623457087087445e-05, 3.622312215156853e-05, 3.6211833503330126e-05, 3.620068309828639e-05, 3.6189692764310166e-05, 3.617885158746503e-05, 3.616817775764503e-05, 3.615764217101969e-05, 3.6147263017483056e-05, 3.613704757299274e-05, 3.6126984923612326e-05, 3.611708234529942e-05, 3.6107321648159996e-05, 3.609772466006689e-05, 3.60882913810201e-05, 3.6079025448998436e-05, 3.606990867410786e-05, 3.606095197028481e-05, 3.605215169955045e-05, 3.6043515137862414e-05, 3.603503864724189e-05, 3.6026725865667686e-05, 3.6018584069097415e-05, 3.601058415370062e-05, 3.600275158532895e-05, 3.599507545004599e-05, 3.598758848966099e-05, 3.598022158257663e-05, 3.597304748836905e-05, 3.5966018913313746e-05, 3.595916132326238e-05, 3.59524528903421e-05, 3.594591180444695e-05, 3.593953078961931e-05, 3.5933309845859185e-05, 3.592724533518776e-05, 3.5921337257605046e-05, 3.591560744098388e-05, 3.591001950553619e-05, 3.590458072721958e-05, 3.5899327485822134e-05, 3.589421248761937e-05, 3.588925756048411e-05] I have tried the examples given in Python gaussian fit on simulated gaussian noisy data, and Fitting (a gaussian) with Scipy vs. ROOT et al without luck. I'm looking to do this with lmfit because it has several advantages. This attempt was done following lmfit documentation, here is the code and plot from numpy import sqrt, pi, exp from lmfit import Model import matplotlib.pyplot as plt def gaussian(x, amp, cen, wid): "1-d gaussian: gaussian(x, amp, cen, wid)" return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2)) gmodel = Model(gaussian) result = gmodel.fit(y, x=x, amp=-0.5, cen=4200, wid=2) plt.plot(x, y,'ro', ms=6) plt.plot(x, result.init_fit, 'g--', lw=2) plt.plot(x, result.best_fit, 'b-', lw=2) So in green is the fit with the initial parameters, and in blue is what should be the best fit, and as you can see I get a gaussian shifted from my points and a straight line. Also, the third row of my data are the errors in the y axis. How can I take the errors into account when fitting the data with lmfit?
The easiest way to do this is probably to make use of the built-in models and combine the GaussianModel and ConstantModel. You can use the errors in the fitting using the keyword 'weights' as described here. You'll probably want to do something like this: import numpy as np from lmfit import Model from lmfit.models import GaussianModel, ConstantModel import matplotlib.pyplot as plt xval = np.array(x) yval = np.array(y) err = np.array(e) peak = GaussianModel() offset = ConstantModel() model = peak + offset pars = offset.make_params(c=np.median(y)) pars += peak.guess(yval, x=xval, amplitude=-0.5) result = model.fit(yval, pars, x=xval, weights=1/err) print(result.fit_report()) plt.plot(xval, yval, 'ro', ms=6) plt.plot(xval, result.best_fit, 'b--')
Duplicate Labels & No Line Appearing in Plot in Python
I'm plotting two lines: a set of experimental data points and a mathematical model. I'm getting the experimental data to plot as expected, but the mathematical model will not plot a line (only the symbol). Additionally, I'm getting duplicate legends and despite trying some of the suggestion I've seen on the site I'm not getting to work (probably I'm not executing it correctly). import numpy as np from sympy import * from sympy import Matrix import matplotlib.pyplot as plt Stretch = [0.998122066, 1.0157277, 1.034507042, 1.052112676, 1.06971831, 1.088497653, 1.106103286, 1.12370892, 1.143661972, 1.160093897, 1.178873239, 1.196478873, 1.214084507, 1.23286385, 1.249295775, 1.266901408, 1.28685446, 1.303286385, 1.322065728, 1.339671362, 1.357276995, 1.374882629, 1.393661972, 1.411267606, 1.430046948, 1.447652582, 1.464084507, 1.48286385, 1.500469484, 1.518075117, 1.535680751, 1.554460094, 1.572065728, 1.59084507, 1.608450704, 1.626056338, 1.643661972, 1.661267606, 1.680046948, 1.697652582, 1.715258216, 1.734037559, 1.751643192, 1.770422535, 1.78685446, 1.805633803, 1.824413146, 1.844366197, 1.860798122, 1.878403756, 1.894835681, 1.912441315, 1.930046948, 1.948826291, 1.967605634, 1.985211268, 2.00399061, 2.021596244, 2.038028169, 2.057981221, 2.075586854, 2.092018779, 2.110798122, 2.128403756, 2.147183099, 2.165962441, 2.183568075, 2.201173709, 2.218779343, 2.237558685, 2.255164319, 2.272769953, 2.291549296, 2.307981221, 2.326760563, 2.344366197, 2.361971831, 2.380751174, 2.398356808, 2.415962441, 2.434741784, 2.452347418, 2.469953052, 2.488732394, 2.505164319] Stress = [0.010526316, 0.010549481, 0.01188998, 0.011913146, 0.012594206, 0.012618915, 0.013299975, 0.013323141, 0.014665184, 0.0153447, 0.016027304, 0.016708364, 0.017389424, 0.018729923, 0.018751544, 0.019432604, 0.019458858, 0.019480479, 0.020163084, 0.020844144, 0.020867309, 0.021548369, 0.022230974, 0.022254139, 0.022278849, 0.023617803, 0.024297319, 0.024979923, 0.025660983, 0.026999938, 0.027023104, 0.027705708, 0.029044663, 0.029069372, 0.030408327, 0.031747282, 0.033086237, 0.034425191, 0.035107796, 0.036446751, 0.037785705, 0.039784099, 0.041123054, 0.042463553, 0.044458858, 0.046457252, 0.048455646, 0.051113479, 0.053108784, 0.055763529, 0.059074623, 0.061729367, 0.065042006, 0.069014085, 0.072986163, 0.077614591, 0.081586669, 0.086872992, 0.092815666, 0.099420867, 0.106680875, 0.114597233, 0.123174574, 0.132408265, 0.142301396, 0.152852422, 0.164059797, 0.177240857, 0.191079812, 0.206236101, 0.22073295, 0.238519274, 0.256307141, 0.273434025, 0.293195577, 0.314929269, 0.335347171, 0.357740301, 0.382105572, 0.406470843, 0.434785026, 0.461123981, 0.488778725, 0.516435014, 0.544088213] c= [6.11739377e+00, 4.78409591e-04] plt.show() for o, d in zip (Stress, Stretch): d1 = d2 = d d3 = 1/(d1*d2) d3 = 1/(d1*d2) C11 = d1**2 C22 = d2**2 C33 = d3**2 p = 4*(c[1]/c[0])*(d3**(c[0]-2)) S11 = -p/C11 + 4*C11*(c[1]/c[0])*(d1**(c[0]-2)) S22 = -p/C22 + 4*C22*(c[1]/c[0])*(d2**(c[0]-2)) T112 = (d1*S11)/(d2*d3) T222 = (d2*S22)/(d1*d3) plt.plot(d, T112, 'g--^', label = 'Model') plt.plot(Stretch, Stress, 'b-o', label = 'Experimental') plt.subplots_adjust(left=0.15) plt.grid(True) plt.ylabel('Stress') plt.xlabel('Applied Stretch') plt.title('Stress as a Function of Applied Stretch') plt.legend() plt.show()
plt.plot should not be included in a loop over the individual data because it work with 2 lists or 2 arrays.As a first step, i've created 2 lists for your model data, then i was able to plot it : import matplotlib.pyplot as plt Stretch = [0.998122066, 1.0157277, 1.034507042, 1.052112676, 1.06971831, 1.088497653, 1.106103286, 1.12370892, 1.143661972, 1.160093897, 1.178873239, 1.196478873, 1.214084507, 1.23286385, 1.249295775, 1.266901408, 1.28685446, 1.303286385, 1.322065728, 1.339671362, 1.357276995, 1.374882629, 1.393661972, 1.411267606, 1.430046948, 1.447652582, 1.464084507, 1.48286385, 1.500469484, 1.518075117, 1.535680751, 1.554460094, 1.572065728, 1.59084507, 1.608450704, 1.626056338, 1.643661972, 1.661267606, 1.680046948, 1.697652582, 1.715258216, 1.734037559, 1.751643192, 1.770422535, 1.78685446, 1.805633803, 1.824413146, 1.844366197, 1.860798122, 1.878403756, 1.894835681, 1.912441315, 1.930046948, 1.948826291, 1.967605634, 1.985211268, 2.00399061, 2.021596244, 2.038028169, 2.057981221, 2.075586854, 2.092018779, 2.110798122, 2.128403756, 2.147183099, 2.165962441, 2.183568075, 2.201173709, 2.218779343, 2.237558685, 2.255164319, 2.272769953, 2.291549296, 2.307981221, 2.326760563, 2.344366197, 2.361971831, 2.380751174, 2.398356808, 2.415962441, 2.434741784, 2.452347418, 2.469953052, 2.488732394, 2.505164319] Stress = [0.010526316, 0.010549481, 0.01188998, 0.011913146, 0.012594206, 0.012618915, 0.013299975, 0.013323141, 0.014665184, 0.0153447, 0.016027304, 0.016708364, 0.017389424, 0.018729923, 0.018751544, 0.019432604, 0.019458858, 0.019480479, 0.020163084, 0.020844144, 0.020867309, 0.021548369, 0.022230974, 0.022254139, 0.022278849, 0.023617803, 0.024297319, 0.024979923, 0.025660983, 0.026999938, 0.027023104, 0.027705708, 0.029044663, 0.029069372, 0.030408327, 0.031747282, 0.033086237, 0.034425191, 0.035107796, 0.036446751, 0.037785705, 0.039784099, 0.041123054, 0.042463553, 0.044458858, 0.046457252, 0.048455646, 0.051113479, 0.053108784, 0.055763529, 0.059074623, 0.061729367, 0.065042006, 0.069014085, 0.072986163, 0.077614591, 0.081586669, 0.086872992, 0.092815666, 0.099420867, 0.106680875, 0.114597233, 0.123174574, 0.132408265, 0.142301396, 0.152852422, 0.164059797, 0.177240857, 0.191079812, 0.206236101, 0.22073295, 0.238519274, 0.256307141, 0.273434025, 0.293195577, 0.314929269, 0.335347171, 0.357740301, 0.382105572, 0.406470843, 0.434785026, 0.461123981, 0.488778725, 0.516435014, 0.544088213] c= [6.11739377e+00, 4.78409591e-04] Stretch_mod=[] Stress_mod=[] for o, d in zip (Stress, Stretch): d1 = d2 = d d3 = 1/(d1*d2) d3 = 1/(d1*d2) C11 = d1**2 C22 = d2**2 C33 = d3**2 p = 4*(c[1]/c[0])*(d3**(c[0]-2)) S11 = -p/C11 + 4*C11*(c[1]/c[0])*(d1**(c[0]-2)) S22 = -p/C22 + 4*C22*(c[1]/c[0])*(d2**(c[0]-2)) T112 = (d1*S11)/(d2*d3) T222 = (d2*S22)/(d1*d3) Stretch_mod.append(d) Stress_mod.append(T112) plt.plot(Stretch_mod, Stress_mod, 'g--^', label = 'Model') plt.plot(Stretch, Stress, 'b-o', label = 'Experimental') plt.subplots_adjust(left=0.15) plt.grid(True) plt.ylabel('Stress') plt.xlabel('Applied Stretch') plt.title('Stress as a Function of Applied Stretch') plt.legend() plt.show()