如何使用 matplotlib 绘制平滑线?


要使用 matplotlib 绘制平滑线,我们可以采取以下步骤 -

步骤

  • 设置图形大小并调整子图像之间和周围的边距。

  • 创建数据点列表,xy

  • 绘制 xy 数据点。

  • 为平滑线创建 x_newbspline 数据点。

  • 获取 y_new 数据点。计算插值 B 样条的(系数)。

  • 使用 plot() 方法绘制 x_newy_new 数据点。

  • 要显示图形,请使用 show() 方法。

示例

Open Compiler
import numpy as np from matplotlib import pyplot as plt from scipy import interpolate # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # x and y data points x = np.array([1, 3, 4, 6, 7]) y = np.array([5, 1, 3, 2, 4]) # Plot the data points plt.plot(x, y) # x_new, bspline, y_new x_new = np.linspace(1, 5, 50) bspline = interpolate.make_interp_spline(x, y) y_new = bspline(x_new) # Plot the new data points plt.plot(x_new, y_new) plt.show()

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

它将生成以下输出 -

更新日期: 2022-02-01

17K+ 查看

开启你的职业生涯

通过完成课程获得认证

开始
广告