Interpolate without having negative values in python
<p>I've been trying to create a smooth line from these values but I can't have negative values in my result. So far all the methods I tried do give negative values. Would love some help.</p>

<pre><code>import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
import numpy as np
y = np.asarray([0,5,80,10,1,10,40,30,80,5,0])
x = np.arange(len(y))

plt.plot(x, y, 'r', ms=5)
spl = UnivariateSpline(x, y)
xs = np.linspace(0,len(y)-1, 1000)
spl.set_smoothing_factor(2)

plt.plot(xs, spl(xs), 'g', lw=3)
plt.show()
</code></pre>

<p><a href="https://i.stack.imgur.com/RY5hW.png" rel="nofollow"><img src="https://i.stack.imgur.com/RY5hW.png" alt="enter image description here"></a></p>

<p>This does it, albeit in some sections better than others.</p>

<pre><code>import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
import numpy as np

y = np.asarray([0,5,80,10,1,10,40,30,80,5,0])
x = np.arange(len(y))

plt.plot(x, y, 'r', ms=5)
spl = UnivariateSpline(x, y)
xs = np.linspace(0,len(y)-1, 1000)
spl.set_smoothing_factor(2)

#new code
ny = spl(xs).clip(0,max(spl(x)))
spl2 = UnivariateSpline(xs, ny)

plt.plot(xs, spl(xs) , 'g', lw=2,label="original")
plt.plot(xs, spl2(xs), 'b', lw=2,label="stack mod")

plt.legend()
plt.show()
</code></pre>

<p><a href="https://i.stack.imgur.com/OO5JD.png" rel="nofollow"><img src="https://i.stack.imgur.com/OO5JD.png" alt="enter image description here"></a></p>

python numpy matplotlib scipy interpolation