python - 使用 PyPlot 绘制平滑线

标签 python matplotlib plot smoothing

我有以下绘制图表的简单脚本:

import matplotlib.pyplot as plt
import numpy as np

T = np.array([6, 7, 8, 9, 10, 11, 12])
power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])

plt.plot(T,power)
plt.show()

就像现在一样,这条线从一个点到另一个点是笔直的,看起来不错,但在我看来可能会更好。我想要的是平滑点之间的线。在 Gnuplot 中,我会使用 smooth cplines 进行绘图。

在 PyPlot 中是否有一种简单的方法可以做到这一点?我找到了一些教程,但它们看起来都相当复杂。

最佳答案

您可以使用 scipy.interpolate.spline 自己平滑数据:

from scipy.interpolate import spline

# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300)  

power_smooth = spline(T, power, xnew)

plt.plot(xnew,power_smooth)
plt.show()

spline is deprecated in scipy 0.19.0, use BSpline class instead.

spline 切换到 BSpline 不是简单的复制/粘贴,需要稍作调整:

from scipy.interpolate import make_interp_spline, BSpline

# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300) 

spl = make_interp_spline(T, power, k=3)  # type: BSpline
power_smooth = spl(xnew)

plt.plot(xnew, power_smooth)
plt.show()

之前: screenshot 1

之后: screenshot 2

关于python - 使用 PyPlot 绘制平滑线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5283649/

相关文章:

r - ggplotify 包中的函数 as.ggplot() 出现奇怪的错误

python - 将 Pandas 数据框从/转换为 ORC 文件

python - 将颜色条添加到极地 contourf 多图

python - 在文件行中查找匹配项,然后转到下一个文件

Matplotlib 绘制一条连续改变颜色的线

python - 如何用一定数量的子图填充图形?

python - 在python中显示二进制文件中的数据

matlab - 在 Matlab 上的两个地理空间点之间的线上绘制箭头

python - 仅使用 32GB 内存分析 80GB 文件中的庞大数据集

python - 在python中查找并打印以特定字符开头和结尾的字符串中的子字符串的索引