Planned smooth line with PyPlot

I have the following simple script that displays 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() 

As now, the line goes straight from point to point, which looks normal, but could be better, in my opinion. I want to smooth the line between the points. In Gnuplot, I would build with smooth cplines .

Is there an easy way to do this in PyPlot? I found several tutorials, but they all seem pretty complicated.

+83
python matplotlib plot
Mar 12 2018-11-11T00:
source share
3 answers

You can use scipy.interpolate.spline to smooth your data yourself:

 from scipy.interpolate import spline xnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.max power_smooth = spline(T,power,xnew) plt.plot(xnew,power_smooth) plt.show() 



The spline is deprecated in scipy 0.19.0, use the Bspline class instead.

Switching from spline to Bspline not a simple copy of / Bspline and requires a little tweaking:

 from scipy.interpolate import make_interp_spline, BSpline xnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.max spl = make_interp_spline(T, power, k=3) #BSpline object power_smooth = spl(xnew) plt.plot(xnew,power_smooth) plt.show() 



Before: screenshot 1

After: screenshot 2

+126
Mar 12 '11 at 17:09
source share

I assume that you mean curve snapping , not smoothing out of the context of your question. PyPlot does not have built-in support for this, but you can easily implement some basic curve settings, for example, the code here , or if you use GuiQwt, it has a curve. (Perhaps you could also steal code from SciPy to do this).

+6
Mar 12 2018-11-17T00:
source share

In this example, the spline works well, but if the function is not smooth in nature and you want a smoothed version, you can also try:

 from scipy.ndimage.filters import gaussian_filter1d ysmoothed = gaussian_filter1d(y, sigma=2) plt.plot(x, ysmoothed) plt.show() 

if you increase sigma, you can get a smoother function.

Proceed with caution with this. It changes the initial values ​​and may not meet your expectations.

+1
Nov 25 '18 at 23:14
source share



All Articles