0

I am having difficulties finding an interpolation for my data points. The line should slightly resemble a negative inverse quadratic (ie like a backwards 'c').

Since this is not a function (x can have multiple values of y), I am not sure what interpolation to use.

I was thinking that perhaps I should flip the axis to create the interpolation points/line using something like UnivariateSpline and then flip it back when I am plotting it?

This is a graph of just the individual points:

scatter plot points of my x-y graph

Here is my code:

import datetime as dt
import matplotlib.pyplot as plt
from scipy import interpolate

file = open_file("010217.hdf5", mode = "a", title = 'Sondrestrom1')
all_data = file.getNode('/Data/Table Layout').read()
file.close()


time = all_data['ut1_unix'] #time in seconds since 1/1/1970
alt = all_data['gdalt'] #all altitude points
electronDens = all_data['nel'] #all electron density points
x = []
y = []
positions = []

for t in range(len(time)): #Looking at this specific time, find all the respective altitude and electron density points
    if time[t] == 982376726:
        x.append(electronDens[t])
        y.append(alt[t])
        positions.append(t)

#FINDING THE DATE        
datetime1970 = dt.datetime(1970,1,1,0,0,0)
seconds = long(time[t])
newDatetime = datetime1970 + dt.timedelta(0, seconds)        
time1 = newDatetime.strftime('%Y-%m-%d %H:%M:%S')
title = "Electron Density vs. Altitude at "
title += time1

plt.plot(x,y,"o")
plt.title(title)
plt.xlabel('Electron Density (log_10[Ne])')
plt.ylabel('Altitude (km)')
plt.show()
user3546200
  • 269
  • 1
  • 3
  • 10
  • Can an y value correspond to multiple x values? – Lynn May 12 '14 at 19:52
  • 4
    You can either just flip the axes, or use a parametric spline (tck, u = [splprep](http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html)([x, y])). – pv. May 12 '14 at 19:57
  • 1
    Rather than post your code verbatim, could you modify it so that your x and y values are entered explicitly? That way it would be a working example that we could run, and post a working solution for you. – Thom Chubb May 13 '14 at 00:19
  • 2
    Also, you state that it's not a function, but clearly x (electron density) is a function of y (height), so interpolation with a spline will be straightforward, you could just use scipy.interpolate.UnivariateSpline(y,x) – Thom Chubb May 13 '14 at 00:21

1 Answers1

0

As the graph heading says "electron density vs. Altidude", I suppose there's only one value per point on the vertical axis?

This means you are actually looking at a function that has been flipped, in order to make the x axis vertical because having altitude on the vertical axis is just more intuitive to humans.

Looking at your code, there seems to have been a measurement where both altitude and electron density were measured. Therefore, even if my theory above is wrong, you should still be able to interpolate everything in the time domain and create a spline from that.

... that's if you really want to have a curve that goes exactly through every point. Seeing as how much scatter there is in the data, you should probably go for a curve fit that doesn't exactly replicate every measurement: scipy.interpolate.Rbf should work alright, and again, for this you should switch the axes, i.e. compute electron density as function of altitude. Just be sure to use smooth=0.01 or maybe a little more (0.0 will exactly go through every point and look a little silly on noisy data).

... actually it seems most of your problem is understanding your data better :)

Zak
  • 3,063
  • 3
  • 23
  • 30