![](/img/trans.png)
[英]Python: pyplot - plot smooth curves with less clutter and show data points on the curve
[英]How to plot smooth curve through the true data points in Python 3?
我已经尝试了一堆已经发布的样条示例,用于在 python 中绘制平滑曲线,但这些平滑曲线并不总是穿过真实点。 到目前为止,我尝试使用make_interp_spline
、 interp1d
和BSpline
。
有什么办法(任何样条选项)我可以 plot 一条必须包含/穿过真实数据点的平滑曲线?
这是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')
您可以轻松验证此插值是否包含真实数据点:
assert np.allclose(fun(x), y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.