How draw a graph with passes certain points and make calculation on it

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

image

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?

1 Like

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:

Figure 2021-09-15 125637

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.

2 Likes

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:

2

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()

4

have you tried writing this function down as a Python function?

1 Like

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()

5

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:

image

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)

1 Like

u helped great bro thank you. i dont know the exact theory but i used polynomial regression and worked.

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

  2. 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,

  1. how can i find for which values graphs give me y=1.35 etc?
  2. 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()

7