hi, i have 14 points in coordinate system. i want to draw a curve passes near this points as picture below. moreover i need to know values of this curve when y=1.45 or etc. thank you
In a way this is more of an applied maths question than a Python question, but maybe we can point you in the right direction.
The most popular Python package to draw graphs like this is matplotlib, and there are many matplotlib tutorials on the web that can get you started.
When it comes to fitting a theoretical function with some free parameters to measured data points, the scipy.optimize module (and, in particular, the scipy.optimize.curve_fit function) are often the right tool for the job.
Depending on what exactly youâ€™re trying to achieve, some form of interpolation may be more useful than curve fitting. See scipy.interpolate for some useful functions.
Understand that as you stated it, â€śa curve [that] passes near [certain] pointsâ€ť is not sufficient information for any program to do anything useful. You will have to define more precisely which curve you require based on what you know about your data. The picture you shared is actually an excellent demonstration of this: there are many things that could happen between the points labelled D and H. As drawn, the function appears to approach positive infinity from both sides, but based on nothing but the points, whoâ€™s to say it doesnâ€™t cross the y axis?
thank you for ur help , i am trying these now. probably i can do graph with the curve_fit function. but how can i extract data from it.
btw in theoricaly my line shouldnt cross y axis. but because of calculation mistake or experiment mistake it can cross. this is a good point how can i say curve_fit fuction to go infinite when close to y axis? thank you again.
last question,is it simple questions to ask here? am i doing wrong ??
i couldnt manage to use curve_fit function. because in description it alvays uses a function . but i dont have any function. i just have a list of values. i tired this:
rom scipy.optimize import curve_fit
def func(x,a,b):
return a*np.exp(b*x)
xData=np.array(L1)
yData=np.array(T1)
plt.plot(xData,yData,"bo", label="exp")
initialGuess=[0.01,5000]
popt, pcov=curve_fit(func, xData, yData, initialGuess)
print(popt)
xFit=np.arange(0.01,0.40,0.01)
plt.plot(xFit,func(xFit,*popt), "r", label="fit")
and result is this:
As I was saying, you need a function. There is an infinite number of possible functions that happen to cross your measured points. You have to tell the computer which one you want.
For example, if (from your knowledge of the problem domain) you happen to know that your data should lie on a parabola, but you donâ€™t know which parabola, you could define a function
def f(x, A, x0, y0):
return A * (x  x0)**2 + y0
and pass that to curve_fit()
. Then curve_fit()
can try to figure out the values of the free parameters A
, x0
, and y0
 and with that, you have your function.
It sounds like you have some idea of what function you expect theoretically. Youâ€™re going to want to write down this knowledge in the form of a function with some number of free parameters for the details you donâ€™t/canâ€™t know.
i put a photo in my first entry. and my expactation is a curve like that. but i dont know how to define this function. i got to point but unfortunately i cant do process. my last prediction like this :D:D:
theorical function is y^2=4*pi^2 *x/9.81 so how can i fit this function to my code
so far i could do this. please help me i m stuck here for almost 4 hours
mymodel = np.poly1d(np.polyfit(X, Y, 5))
myline = np.linspace(0.35, 0.35 , 100)
plt.scatter(X, Y)
plt.plot(myline, mymodel(myline))
plt.show()
have you tried writing this function down as a Python function?
it is not correct. i missunderstood problem. i dont know theorical equation. but i am prety close to solution with this code. i need a few altereation. do u have any idea to how can i make this graph go infinity when x=0?
mymodel = np.poly1d(np.polyfit(X, Y, 6))
myline = np.arange(0.35, 0.35 , 0.001)
plt.scatter(X, Y)
plt.plot(myline, mymodel(myline))
plt.title("6 derece")
plt.show()
As you have discovered, the more free parameters you add to your fit (and this is what youâ€™re doing by increasing the order of the polynomial) the better you can match your data, because, with enough free parameters, you can fit anything. Add a few more orders to the polynomial and you should be able to get a curve that passes through each point perfectly. Any results between your datapoints, however, would be completely meaningless. Look at the sharp peak in the curve between the first two datapoints on your most recent plot:
Can you conclude there should be a peak here based on the data? Clearly not. Does putting the peak here help with getting this particular function close to the data? Absolutely!
You cannot get a meaningful result without some theory.
I donâ€™t mean to sound too negative, here. Youâ€™re going in the right direction, and it looks like youâ€™ve achieved a fair bit! However, the next step is not to fiddle with Python code. The next step is to take a step back and think / learn a bit about the science behind the function youâ€™re trying to draw.
Also think about exactly youâ€™re actually trying to achieve. If you want a smooth function describing some physical process, well, as I was saying, look into the science. If, on the other hand, you just need some values between the points and simply drawing a bunch of straight lines from one point to the next is â€śclose enoughâ€ť, well, then it may be simplest to just draw a bunch of straight lines between one point and the next (a.k.a. interpolate)
u helped great bro thank you. i dont know the exact theory but i used polynomial regression and worked.

i split my result 2 from middle and train python separately. it really improve my graph probably because of get rid of minus sign.

i made a mistake to predict all values from my experimental values. i cahange it with np.linspace(0,0.35,100) and it Ĺźmprove my graphic also.
now it is good enough to make calculation but i have 2 more question which is,
 how can i find for which values graphs give me y=1.35 etc?
 as you see in the picture my graph not goes infinity when close to x=0 . how can i manage it
thank you
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
poly_reg = PolynomialFeatures(degree = 4)
X2=np.array(L1).reshape(1,1)
Y=np.array(T1)
x_poly = poly_reg.fit_transform(X2)
lin_reg2 = LinearRegression()
lin_reg2.fit(x_poly,Y)
plt.scatter(X2,Y,color = 'red')
xval=np.linspace(0,0.35,100).reshape(1,1)
plt.plot(xval,lin_reg2.predict(poly_reg.fit_transform(xval)), color = 'blue')
poly_reg = PolynomialFeatures(degree = 4)
X2=np.array(L2).reshape(1,1)
Y=np.array(T2)
x_poly = poly_reg.fit_transform(X2)
lin_reg2 = LinearRegression()
lin_reg2.fit(x_poly,Y)
plt.scatter(X2,Y,color = 'red')
xval=np.linspace(0.35,0,100).reshape(1,1)
plt.plot(xval,lin_reg2.predict(poly_reg.fit_transform(xval)), color = 'green')
plt.title("4 degree poly reg")
plt.show()