Create Scatter Plot with smooth Line using Python
A curve can be smoothened to reach a well approximated idea of the visualization. In this article, we will be plotting a scatter plot with the smooth line with the help of the SciPy library. To plot a smooth line scatter plot we use the following function:
- scipy.interpolate.make_interp_spline() from the SciPy library computes the coefficients of interpolating B-spline. By importing, this function from the Scipy library and added the parameter, It is quite easier to get the smooth line to scatter plot.
scipy.interpolate.make_interp_spline(x, y, k=3, t=None, bc_type=None, axis=0, check_finite=True)
- k:-B-spline degree
- bc_type:-Boundary conditions
- axis:-Interpolation axis
- check_finite:-Whether to check that the input arrays contain only finite numbers
Return: a BSpline object of the degree k and with knots t.
- np.linspace() function is imported from NumPy library used to get evenly spaced numbers over a specified interval used to draw a smooth line scatter plot.
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
- start:-The starting value of the sequence.
- stop:-The end value of the sequence.
- num:-Number of samples to generate.
- endpoint:-If True, stop is the last sample.
- retstep:-If True, return (samples, step), where the step is the spacing between samples.
- dtype:-The type of the output array.
- axis:- The axis in the result to store the samples.
Return: A array of num equally spaced samples in the closed interval
- Import module
- Create or load data
- Create a scatter plot
- Create a smoothened curve from the points of the scatter plot
- Display plot
Let us start with a sample scatter plot.
Now let’s visualize the scatter plot by joining points of the plot so that an uneven curve can appear i.e. without smoothening so that difference can be apparent.
Now, We will be looking at the same example as above with the use of np.linspace() and scipy.interpolate.make_interp_spline() function.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course