Plot smooth line with PyPlot
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 Question
00:47 Accepted answer (Score 221)
01:45 Answer 2 (Score 65)
02:17 Answer 3 (Score 21)
03:09 Answer 4 (Score 12)
03:43 Thank you
--
Full question
https://stackoverflow.com/questions/5283...
Accepted answer links:
[image]: https://i.stack.imgur.com/dSLtt.png
[image]: https://i.stack.imgur.com/olGAh.png
Answer 3 links:
[scipy.interpolate]: https://docs.scipy.org/doc/scipy/referen...
Answer 4 links:
[implementations]: https://stackoverflow.com/a/49357445/985...
[image]: https://i.stack.imgur.com/C5wKH.png
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #plot #smoothing
#avk47
ACCEPTED ANSWER
Score 230
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()
spline is deprecated in scipy 0.19.0, use BSpline class instead.
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()
ANSWER 2
Score 72
For this example spline works well, but if the function is not smooth inherently and you want to have 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 more smoothed function.
Proceed with caution with this one. It modifies the original values and may not be what you want.
ANSWER 3
Score 26
See the scipy.interpolate documentation for some examples.
The following example demonstrates its use, for linear and cubic spline interpolation:
import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import interp1d # Define x, y, and xnew to resample at. x = np.linspace(0, 10, num=11, endpoint=True) y = np.cos(-x**2/9.0) xnew = np.linspace(0, 10, num=41, endpoint=True) # Define interpolators. f_linear = interp1d(x, y) f_cubic = interp1d(x, y, kind='cubic') # Plot. plt.plot(x, y, 'o', label='data') plt.plot(xnew, f_linear(xnew), '-', label='linear') plt.plot(xnew, f_cubic(xnew), '--', label='cubic') plt.legend(loc='best') plt.show()
Slightly modified for increased readability.
ANSWER 4
Score 12
Here is a simple solution for dates:
from scipy.interpolate import make_interp_spline
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as dates
from datetime import datetime
data = {
datetime(2016, 9, 26, 0, 0): 26060, datetime(2016, 9, 27, 0, 0): 23243,
datetime(2016, 9, 28, 0, 0): 22534, datetime(2016, 9, 29, 0, 0): 22841,
datetime(2016, 9, 30, 0, 0): 22441, datetime(2016, 10, 1, 0, 0): 23248
}
#create data
date_np = np.array(list(data.keys()))
value_np = np.array(list(data.values()))
date_num = dates.date2num(date_np)
# smooth
date_num_smooth = np.linspace(date_num.min(), date_num.max(), 100)
spl = make_interp_spline(date_num, value_np, k=3)
value_np_smooth = spl(date_num_smooth)
# print
plt.plot(date_np, value_np)
plt.plot(dates.num2date(date_num_smooth), value_np_smooth)
plt.show()



