Python: Plotting Smooth Curves

Featured Img Smooth Curve

Hey, there fellow learner! Today we are going to learn how to build smooth curves using matplotlib and SciPy module.

Sometimes, you wish to get smooth curves for data visualization to make the plots look better and elegant. Fortunately, the same can be achieved with the help of matplotlib and SciPy module.

Recommended read: Create animated plots in Python

1. Importing Modules

The first step is to import the required modules into our program. The modules that we are going to achieve our goal numpy, matplotlib and SciPy modules where numpy is required for data preparation, matplotlib for plotting simple plots, and SciPy to help out with smooth curves.

import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt

2. Data Preparation

To prepare data we would be using the numpy arrays as they are easier to handle. We would be plotting a sine wave where x coordinates are the x-axis value and y coordinates are the sine value of x.

x = np.array([i for i in range(50)])
y = np.array([np.sin(i) for i in x])

3. Making B-spline Curve

To get a smooth curve we make use of the make_interp_spline function to get a B-spline curve by passing the x and y arrays. It returns the x and y coefficients of the curve together.

The next thing we need to do is to separate the coefficients from each other. The code below does the same for you.

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

4. Plotting a dataset

Now the last step is to plot the plot using the matplotlib library and the necessary functions and configurations to make the plot look better.

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="red")
plt.title("Smooth Sine Wave")
plt.xlabel("x values")
plt.ylabel("y values")
plt.show()

The final output plot is shown below.

Smooth B Spline Curve
Smooth B Spline Curve

Another Illustration

The following code plots a few more smooth curves together with the help of subplot function of matplotlib.

plt.figure(figsize=(15,15))

plt.subplot(3, 3, 1)
x = np.array([i for i in range(30)])
y = np.array([np.tan(i) for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="red")
plt.title("Smooth Tan Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.subplot(3, 3, 2)
x = np.array([i for i in range(30)])
y = np.array([np.exp(i) for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="green")
plt.title("Smooth e^x Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.subplot(3, 3, 3)
x = np.array([i for i in range(10)])
y = np.array([np.sqrt(i) for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="pink")
plt.title("Smooth sqrt Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.subplot(3, 3, 4)
x = np.array([i for i in range(30)])
y = np.array([5**i for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="black")
plt.title("Smooth 5^x Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.subplot(3, 3, 5)
x = np.array([i for i in range(-10,10,1)])
y = np.array([i**2 -5 for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="orange")
plt.title("Smooth X^2-5 Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.subplot(3, 3, 6)
x = np.array([i for i in range(30)])
y = np.array([3**i + i for i in x])

B_spline_coeff = make_interp_spline(x, y)
X_Final = np.linspace(x.min(), x.max(), 500)
Y_Final = B_spline_coeff(X_Final)

plt.style.use('seaborn')
plt.plot(X_Final,Y_Final,color="magenta")
plt.title("Smooth 3^x+x Wave")
plt.xlabel("x values")
plt.ylabel("y values")

plt.show()

The output of the code is displayed below.

Multiple B Spline Curve
Multiple B Spline Curve

Conclusion

Congratulations! Today we learned plotting perfect smooth curves plots using matplotlib and SciPy modules. You can try out various other examples as well.

Happy coding!

Thank you for reading!