I have plotted this data using matplotlib on a log-log plot. I am trying to fit first 5-6 points of my data (low q regime) with a straight line using polyfit
. I have tried the solution in this previously answered question. If I try that, curve doesn't even appear on my plot.
Here is my code :
import matplotlib.pyplot as plt
import numpy as np
from scipy import *
with open("data.dat", "r") as f:
x = []
y = []
for line in f:
if not line.strip() or line.startswith('@') or line.startswith('#'):
continue
row = line.split()
x.append(float(row[0]))
y.append(float(row[1]))
x = np.asarray(x)
y = np.asarray(y)
plt.loglog(x, y, basex=10,basey=10, linestyle="none",marker=".",color='b',label="data")
x_fit= x[:5]
y_fit= 1/x_fit**4/10**6
plt.plot(x_fit,y_fit, label='should be like this',color='r')
m,b = np.polyfit(x_fit, y_fit, 1)
plt.plot(x_fit, m*x_fit+ b, linestyle='--', label='polyfit', color='g')
plt.legend()
plt.ylabel('Spectra ($\AA^2$)')
plt.xlabel('q ($\AA^{-1}$)')
plt.grid()
plt.show()
Edit
If I extract those point in new file and plot it with a normal x-y plot. polyfit
works fine.