0

Consider the graph and table below. We can see that because the table's data points are ordered top to bottom, the graph's data points, when plotted as connecting lines, connect in the same ordered manner. I am wondering if it is possible to linearly interpolate the ordered data and generate additional point values along the blue lines connecting each point.

Value1     Value2
1          5    
4          4
2          6
3          6

plt.plot(df['value1'],df['value2'])

enter image description here

I have attempted to use scipy's 1d interpolator. However, based on my script, scipy does not consider the dataframe's order. Instead it considers each data point unordered and interpolates logically to generate a function. I am wondering if there is a method to obtain additional data from the plot above.

ut=np.array(df['value1'])
vt=np.array(df['value2'])

function=interpolate.interp1d(ut, vt, kind='linear')
new_x = np.linspace(1, 4, num=100, endpoint=True)
plt.plot(new_x,function(new_x),'o')

enter image description here

Bob
  • 115
  • 10
  • `plt.plot()` draws a line from (1, 5) to (4, 4), then another line from (4, 4) to (2, 6) and then one last from (2, 6) to (3, 7). Mathematically speaking if the mapping x -> y is not unique, it is not a function. If you were to order these pairs according to their x value, you're get (1, 5), (2, 6), (3, 7) and (4, 4). Which is also what the interpolation gives you. But say you want the ordering according to the top plot, then I ask you what value you expect the interpolation to spit out when you request `function(2.5)`, since that value for x runs is present in all 3 lines. – Reti43 Oct 16 '21 at 15:34
  • Hello, I am aware that the first graph is not a function. What I should have asked is if it is possible to linearly interpolate between each dataframe value. That way, there would be values between (1,5) to (4,4) and then values from (4,4) to (2,6) and so on without needing a graph. The graph is simply for display. – Bob Oct 16 '21 at 15:50
  • Okay, so for x = 2.5 you want 3 values, one for each line. Are you specifically looking for interpolation or extrapolation? For example, x = 1.1 is only inside the line segment (1,5)-(4,4), so that would only return one value. And for x = -1, you'd get nothing. Is that what you want? – Reti43 Oct 16 '21 at 22:52
  • Hello, after browsing around I believe I found a simple method to resolve my issue. I can add NaN values (such as this example post: https://stackoverflow.com/questions/66466080/python-pandas-insert-empty-rows-after-each-row) and then run pandas.DataFrame.interpolate, which is a simple interpolator that replaces NaN values with numbers between each original data value. I tested it and it seems to work fine! – Bob Oct 17 '21 at 02:42
  • Feel free to write an answer with your solution. It might be helpful to future readers. – Reti43 Oct 17 '21 at 21:03

0 Answers0