简体   繁体   中英

How to plot smooth curve through the true data points in Python 3?

I have tried a bunch of spline examples already posted for plotting smooth curves in python but those smoothed curves don't always cross through the true points. So far, I tried using make_interp_spline , interp1d and also BSpline .

Is there any way (any spline option) I can plot a smooth curve that must include/cross through true data points?

Here is a simple example with interp1d :

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

x = np.arange(5)
y = np.random.random(5)
fun = interp1d(x=x, y=y, kind=2)
x2 = np.linspace(start=0, stop=4, num=1000)
y2 = fun(x2)

plt.plot(x2, y2, color='b')
plt.plot(x, y, ls='', marker='o', color='r')

随机样条插值

You can easily verify that this interpolation includes the true data points:

assert np.allclose(fun(x), y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM