用PyPlot绘制平滑线 [英] Plot smooth line with PyPlot
问题描述
我有以下简单的脚本可以绘制图形:
I've got the following simple script that plots a graph:
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
进行绘制.
As it is now, the line goes straight from point to point which looks ok, but could be better in my opinion. What I want is to smooth the line between the points. In Gnuplot I would have plotted with smooth cplines
.
在PyPlot中有一种简单的方法吗?我已经找到了一些教程,但是它们看起来都相当复杂.
Is there an easy way to do this in PyPlot? I've found some tutorials, but they all seem rather complex.
推荐答案
您可以使用scipy.interpolate.spline
自己整理数据:
You could use scipy.interpolate.spline
to smooth out your data yourself:
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()
scipy 0.19.0中已弃用spline,请改用BSpline类.
spline is deprecated in scipy 0.19.0, use BSpline class instead.
从spline
切换到BSpline
并不是简单的复制/粘贴操作,需要进行一些调整:
Switching from spline
to BSpline
isn't a straightforward copy/paste and requires a little tweaking:
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()
之前:
Before:
之后:
这篇关于用PyPlot绘制平滑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!