8. smoothcurve
¶
Module for defining the class related to the curve softener.
-
class
smoothcurve.
SmoothCurve
(x, y, k=3, n=300)[source]¶ Bases:
object
Creates an instance of an object that defines a curve smoother than the input through the \(k\)-order B-Spline method for interpolation.
SmoothCurve(x, y, k=3, n=300)
-
x
¶ abscisses of the curve to smooth.
Type: tuple, list or numpy.ndarray
-
y
¶ ordinates of the curve to smooth. It must have the same length of
x
.Type: tuple, list or numpy.ndarray
-
k
¶ interpolation order.
Type: int
-
n
¶ number of points of the returned smooth curve
Type: int
Note
The class
SmoothCurve
requires numpy, matplotlib. and scipy.Examples
>>> from pybimstab.smoothcurve import SmoothCurve >>> x = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 0, 0, 0, 0] >>> y = [9, 8, 7, 7, 8, 8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> curve = SmoothCurve(x=x, y=y) >>> curve.__dict__.keys() dict_keys(['x', 'y', 'k', 'n', 'smoothing'])
-
smooth
()[source]¶ Method to generate a smooth curve from the points input through the \(k\)-order B-Spline method.
Returns: \(\left(2 \times n \right)\) array where \(n\) is the number of nodes where the path has crossed; the first row of the array contains the abscisses and the second one contains the ordinates of the nodes into the grid-graph. Return type: (numpy.ndarray) Examples
>>> from pybimstab.smoothcurve import SmoothCurve >>> x = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 0, 0, 0, 0] >>> y = [9, 8, 7, 7, 8, 8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> curve = SmoothCurve(x=x, y=y, n=10) >>> curve.smooth() array([[9. , 7.56984454, 6.11111111, 4.66666667, 3.2222222, 1.77960677, 1.00617284, 0.77206219, 0.01463192, 0. ], [9. , 7.05749886, 7.77206219, 8. , 7.9215821, 6.77777778, 5.33333333, 3.88888889, 2.43015546, 0. ]])
-
plot
()[source]¶ Method for generating a graphic of the
smooth
method output. It allows visually to compare the no smoothed line and its smooothed version.Returns: object with the matplotlib structure of the plot. You might use it to save the figure for example. Return type: (matplotlib.figure.Figure) Examples
>>> from pybimstab.smoothcurve import SmoothCurve >>> x = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 0, 0, 0, 0] >>> y = [9, 8, 7, 7, 8, 8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> curve = SmoothCurve(x, y, 1) >>> fig = curve.plot()
>>> import numpy as np >>> from pybimstab.smoothcurve import SmoothCurve >>> x = np.linspace(0, 2*np.pi, 50) >>> y = np.sin(x) + np.random.random(50) * 0.5 >>> for k in [0, 2, 15]: >>> curve = SmoothCurve(x, y, k) >>> fig = curve.plot()
-