Fit a curve to the boundary of a scatterplot
<p>I'm trying to fit a curve to the boundary of a scatterplot. <a href="https://i.stack.imgur.com/OMTHl.png" rel="noreferrer">See this image for reference</a>.
<a href="https://i.stack.imgur.com/OMTHl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OMTHl.png" alt="enter image description here"></a></p>

<p>I have accomplished a fit already with the following (simplified) code. It slices the dataframe into little vertical strips, and then finds the minimum value in those strips of width <code>width</code>, ignoring <code>nan</code>s. (The function is monotonically decreasing.)</p>

<pre><code>def func(val):
    """ returns some function of 'val'"""
    return val * 2

for i in range(0, max_val, width)):
    _df = df[(df.val &gt; i) &amp; (df.val &lt; i + width)] # vertical slice
    if np.isnan(np.min(func(_df.val)):            # ignore nans
        continue
    xs.append(i + width)                         
    ys.append(np.min(func(_df.val)))
</code></pre>

<p>I am then doing the fit with <code>scipy.optimize.curve_fit</code>. My question is: is there a more natural or pythonic way to do this -- and is there any way I can bump up the accuracy? (for example, by giving a higher weighting to areas of the scatterplot with a higher density of points?)</p>