How to plot a smooth line with matplotlib?

To plot a smooth line with matplotlib, you can use interpolation techniques to create a curve that smoothly connects your data points. The most effective approach is using B-spline interpolation from SciPy.

Basic Smooth Line Plotting

Here's how to create a smooth line from discrete data points ?

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

# Set figure size
plt.figure(figsize=(10, 6))

# Original data points
x = np.array([1, 3, 4, 6, 7])
y = np.array([5, 1, 3, 2, 4])

# Plot original data points
plt.plot(x, y, 'o-', label='Original Data', linewidth=2, markersize=8)

# Create smooth line using B-spline interpolation
x_smooth = np.linspace(x.min(), x.max(), 300)
bspline = interpolate.make_interp_spline(x, y, k=3)  # k=3 for cubic spline
y_smooth = bspline(x_smooth)

# Plot smooth line
plt.plot(x_smooth, y_smooth, '-', label='Smooth Line', linewidth=2)

plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Smooth Line Plot using B-spline Interpolation')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Different Smoothing Methods

Method 1: Using B-spline (Recommended)

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

x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 3, 1, 4, 2, 5])

# B-spline interpolation
x_smooth = np.linspace(0, 5, 100)
bspline = interpolate.make_interp_spline(x, y, k=3)
y_smooth = bspline(x_smooth)

plt.figure(figsize=(8, 5))
plt.plot(x, y, 'ro', label='Data Points', markersize=8)
plt.plot(x_smooth, y_smooth, 'b-', label='B-spline Smooth', linewidth=2)
plt.legend()
plt.title('B-spline Interpolation')
plt.show()

Method 2: Using Cubic Spline

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

x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 3, 1, 4, 2, 5])

# Cubic spline interpolation
cs = CubicSpline(x, y)
x_smooth = np.linspace(0, 5, 100)
y_smooth = cs(x_smooth)

plt.figure(figsize=(8, 5))
plt.plot(x, y, 'ro', label='Data Points', markersize=8)
plt.plot(x_smooth, y_smooth, 'g-', label='Cubic Spline', linewidth=2)
plt.legend()
plt.title('Cubic Spline Interpolation')
plt.show()

Comparison of Methods

Method Best For Pros Cons
B-spline General purpose smoothing Flexible, customizable degree Requires SciPy
Cubic Spline Natural-looking curves Smooth derivatives May oscillate with noisy data
Linear Interpolation Simple connections Fast, simple Not smooth

Key Parameters

  • k parameter: Degree of spline (1=linear, 2=quadratic, 3=cubic)

  • Number of points: More points in x_smooth create smoother curves

  • Data range: Keep interpolation within original data range

Conclusion

Use B-spline interpolation with make_interp_spline() for the best smooth line plots. Increase the number of interpolation points for smoother curves, and choose the appropriate spline degree based on your data characteristics.

Updated on: 2026-03-26T18:59:55+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements