I am plotting some points and to make the plot smoother I'm using 'make_interp_spline'. The problem is that I get a falling line between the first two points. The other points are ok, and they have the same distance. Is there something I can do about it?
Ps: I just have the data from 10 and 30.
My code:
import matplotlib
import pylab as pl
from pylab import *
import numpy as np
from scipy.interpolate import make_interp_spline
x1, y1 = np.loadtxt('Grafico_I_D_Fe.txt', dtype=float, delimiter=',', unpack=True)
B_spline_coeff1 = make_interp_spline(x1, y1)
X1_Final = np.linspace(x.min(), x.max(), 500)
Y1_Final = B_spline_coeff1(X1_Final)
pl.plot(X1_Final, Y1_Final, color='r', label=r'Iron ($2 \times 10^7$)')
pl.scatter(x1, y1, marker='*', color='r')
Data:
10, 3.187170532786709e-15
30, 1.031832270812154e-13
50, 3.444779276423242e-13
70, 6.316369610195546e-14
Plot:
CodePudding user response:
Interpolation functions often have the unintuitive property of wiggling a lot. In your case, the interpolated curve goes below zero, although the data points don't.
Many other interpolation or approximation functions exist, each having their own benefits and drawbacks for specific situations. PchipInterpolator
is one such function, that tries to stay inside the range of the data points (but has a less nice second derivative).
PS: It seems that for these specific data points, the original make_interp_spline
works even better than PchipInterpolator
when using the log conversion. Often this won't be the case, and you might need to consider other interpolation or approximation functions.
B_spline_coeff1 = make_interp_spline(x1, np.log(y1))
X1_Final = np.linspace(9, 71, 500)
Y1_Final = np.exp(B_spline_coeff1(X1_Final))
plt.plot(X1_Final, Y1_Final, color='r', label=r'Iron ($2 \times 10^7$)')
plt.scatter(x1, y1, marker='*', color='r')
plt.yscale('log')
plt.show()