title: Find minimum distance from point to complicated curve
body: <p>I have a complicated curve defined as a set of points in a table like so (the full table is <a href="http://pastebin.com/1w1dUXQy" rel="noreferrer">here</a>):</p>
  
  <pre><code>#  x   y
  1.0577  12.0914
  1.0501  11.9946
  1.0465  11.9338
  ...
  </code></pre>
  
  <p>If I plot this table with the commands:</p>
  
  <pre><code>plt.plot(x_data, y_data, c='b',lw=1.)
  plt.scatter(x_data, y_data, marker='o', color='k', s=10, lw=0.2)
  </code></pre>
  
  <p>I get the following:</p>
  
  <p><img src="https://i.stack.imgur.com/5Tdsx.png" alt="enter image description here"></p>
  
  <p>where I've added the red points and segments manually. What I need is a way to calculate those segments for each of those points, that is: <em>a way to find the minimum distance from a given point in this 2D space to the interpolated curve</em>.</p>
  
  <p>I can't use the distance to the data points themselves (the black dots that generate the blue curve) since they are not located at equal intervals, sometimes they are close and sometimes they are far apart and this deeply affects my results further down the line.</p>
  
  <p>Since this is not a well behaved curve I'm not really sure what I could do. I've tried interpolating it with a <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html" rel="noreferrer">UnivariateSpline</a> but it returns a very poor fit:</p>
  
  <pre><code># Sort data according to x.
  temp_data = zip(x_data, y_data)
  temp_data.sort()
  # Unpack sorted data.
  x_sorted, y_sorted = zip(*temp_data)
  
  # Generate univariate spline.
  s = UnivariateSpline(x_sorted, y_sorted, k=5)
  xspl = np.linspace(0.8, 1.1, 100)
  yspl = s(xspl)
  
  # Plot.
  plt.scatter(xspl, yspl, marker='o', color='r', s=10, lw=0.2)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/uzRLZ.png" alt="enter image description here"></p>
  
  <p>I also tried increasing the number of interpolating points but got a mess:</p>
  
  <pre><code># Sort data according to x.
  temp_data = zip(x_data, y_data)
  temp_data.sort()
  # Unpack sorted data.
  x_sorted, y_sorted = zip(*temp_data)
  
  t = np.linspace(0, 1, len(x_sorted))
  t2 = np.linspace(0, 1, 100)    
  # One-dimensional linear interpolation.
  x2 = np.interp(t2, t, x_sorted)
  y2 = np.interp(t2, t, y_sorted)
  plt.scatter(x2, y2, marker='o', color='r', s=10, lw=0.2)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xwXTt.png" alt="enter image description here"></p>
  
  <p>Any ideas/pointers will be greatly appreciated.</p>