Python Forum
how to get x values based on y-axis values from curvefit function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get x values based on y-axis values from curvefit function
#1
Hi,

I have fitted a function for extrapolation and now I would like to get the x value when y is of a certain value. How do I get this?

def func(x, b1, b2):
    return b1*np.exp(-b2*(x))

x = np.array([47,77,103,129,152,177,207,234,255,282,311,337,358,382,416,440,463,490,512,535,566,573])
y = np.array([0.904996,0.946489,0.931208,0.930137,0.915004,0.92138,0.911744,0.908241,0.894834,0.895659,0.897973,0.885373,0.895916,0.883586,0.880162,0.857255,0.872408,0.87055,0.855421,0.854406,0.852419,0.853073])

popt, pcov = curve_fit(func, x, y, p0=[0.1,0.1])

plt.plot(x, y, 'x')
xx = np.linspace(0, 2000, 100)
yy = func(xx, *popt)
plt.plot(xx, yy, lw=1)
Thanks.
Reply
#2
This problem can be solved in many ways, e.g. consider the following:

from scipy.optimize import root_scalar
root_scalar(lambda x: func(x, *popt) - your_y_value, bracket=[0, 10000], method='bisect')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning conditional values in Pandas Scott 3 725 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  attempt to split values from within a dataframe column mbrown009 8 2,217 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  Increase df column values decimals SriRajesh 2 1,084 Nov-14-2022, 05:20 PM
Last Post: deanhystad
  replace sets of values in an array without using loops paul18fr 7 1,629 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  Changing Values in a List DaveG 1 1,248 Apr-04-2022, 03:38 PM
Last Post: jefsummers
Question How does one clean a populated table in MySQL/MariaDB? Copying values across tables? BrandonKastning 2 1,539 Jan-17-2022, 05:46 AM
Last Post: BrandonKastning
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,234 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 1,756 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Write a dictionary with arrays as values into JSON format paul18fr 3 5,510 Oct-20-2021, 10:38 AM
Last Post: buran
  Remove specific values from dataframe jonah88888 0 1,687 Sep-24-2021, 05:09 AM
Last Post: jonah88888

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020