Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data interpolation in python

I have four one dimensional lists: X1, Y1, X2, Y2.

  • X1 and Y1 each have 203 data points.
  • X2 and Y2 each have 1532 data points.
  • X1 and X2 are at different intervals, but both measure time.

I want to graph Y1 vs Y2.

I can plot just fine once I get the interpolated data, but can't think of how to interpolate data. I've thought and researched this a couple hours, and just can't figure it out. I don't mind a linear interpolation, but just can't figure out a way.

like image 501
Jessica Coombs Avatar asked Mar 10 '26 06:03

Jessica Coombs


1 Answers

I think this is what you want:

import numpy as np
import matplotlib.pyplot as plt

# first data set
X1 = np.linspace(0,1,203)
Y1 = np.sin(X1)

# second data set
X2 = np.linspace(0, 0.5, 1532)
Y2 = np.cos(X2)

# get interpolated values of Y1 evaluated at X2
Y1_interp = np.interp(X2, X1, Y1)

# plot interpolated Y1 vs Y2
plt.plot(Y1_interp, Y2)
plt.show()
like image 57
John Kitchin Avatar answered Mar 12 '26 20:03

John Kitchin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!