失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matplotlib绘制平滑曲线

matplotlib绘制平滑曲线

时间:2022-11-27 15:27:18

相关推荐

matplotlib绘制平滑曲线

在不使用曲线拟合,改变原值的条件下,有两种方法:

方法1 interp1d

def smooth_xy(x_value: np.ndarray, y_value: np.ndarray):from scipy.interpolate import interp1dcubic_interploation_model = interp1d(x_value, y_value, kind="cubic")x_smooth = np.linspace(x_value.min(), x_value.max(), 500)y_smooth = cubic_interploation_model(x_smooth)return x_smooth, y_smooth

方法2 make_interp_spline

def smooth_xy(x_value: np.ndarray, y_value: np.ndarray):from scipy.interpolate import make_interp_splinemodel = make_interp_spline(x_value, y_value)x_smooth = np.linspace(x_value.min(), x_value.max(), 500)y_smooth = model(x_smooth)return x_smooth, y_smooth

使用示例

import numpy as npfrom scipy.interpolate import make_interp_splineimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rcParams['font.family'] = ['Heiti TC'] # 显示中文def smooth_xy(x_value: np.ndarray, y_value: np.ndarray):model = make_interp_spline(x_value, y_value)x_smooth = np.linspace(x_value.min(), x_value.max(), 500)y_smooth = model(x_smooth)return x_smooth, y_smoothif __name__ == '__main__':# 生成数据x = np.array(list(range(40)))y = np.random.random(40)# 绘制原始曲线plt.plot(x, y, label="原始")# 绘制平滑曲线x, y = smooth_xy(x, y)plt.plot(x, y, label="平滑")plt.title("平滑曲线与原始曲线对比图")plt.xlabel("X")plt.ylabel("Y")plt.legend()plt.show()

效果图:

如果觉得《matplotlib绘制平滑曲线》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。