Open In App

How to Plot a Smooth Curve in Matplotlib?

Last Updated : 31 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Many times, we have line plots generated from lists of data that are quite scattered which makes graphs seem like straight lines connecting dots or quite dense which leads to the data points being extremely close to each other and hence the plot looks cluttered.  The matplotlib.pyplot.plot() function by default produces a curve by joining two adjacent points in the data with a straight line and hence the matplotlib.pyplot.plot() function does not produce a smooth curve for a small range of data points.

Syntax:

sns.heatmap(data, cmap=None, annot=None, linewidths=0, linecolor='white', cbar=True, square=False, **kwargs)

Parameters:

  • data: 2D data (e.g., NumPy array or DataFrame) to be represented as a heatmap.
  • cmap: Colormap used to assign colors to values.
  • annot: If True, writes data values in each cell.
  • linewidths: Width of the lines dividing cells.
  • linecolor: Color of the dividing lines between cells.
  • cbar: Whether to display the color bar.
  • square: If True, makes the cells square-shaped.

Returns: It returns a matplotlib Axes object with the smoothed heatmap drawn on it.

Let see an example: We will use the tips dataset which is an inbuilt dataset. This dataset contains information about restaurant tips, total bill amount, tip amount, customer details like sex and day of the week etc. Also, we will be using Seaborn and Matplotlib libraries for this.

Python
import numpy as np
import matplotlib.pyplot as plt

# Dataset
x = np.array([ 1,  2,  3,  4,  5,  6,  7, 8 ])
y = np.array([ 20, 30, 5, 12, 39, 48, 50, 3 ])

# Plotting the Graph
plt.plot(x, y)
plt.title("Curve plotted using the given points")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Output:

Example 1: Smooth Spline Curve with PyPlot:

We draw a smooth spline curve using scipy.interpolate.make_interp_spline(), which fits a smooth curve through the given data points. To make the curve appear smooth, we generate many closely spaced x-values using np.linspace(), which creates evenly spaced numbers in a range. By setting the num parameter to a high value like 500, we get a smooth curve between the minimum and maximum x-values.

Syntax:

numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None, axis = 0)

X_Y_Spline = scipy.interpolate.make_interp_spline(x, y)

Parameters:

  • start, stop: The starting and ending values of the sequence.
  • num: Number of samples to generate (default is 50). A higher number results in a smoother curve.
  • endpoint: If True (default), stop is the last sample; otherwise, it is not included.
  • retstep: If True, return (samples, step), where step is the spacing between samples.
  • dtype: The data type of the output array.
  • axis: The axis in the result along which the linspace samples are stored.

Returns:

  • np.linspace() returns a NumPy array of evenly spaced values.
  • make_interp_spline() returns a callable spline function that can be used to evaluate smooth y-values from new x-values.

Example:

Python
import numpy as np

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

# Dataset
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([20, 30, 5, 12, 39, 48, 50, 3])

X_Y_Spline = make_interp_spline(x, y)

# Returns evenly spaced numbers
# over a specified interval.
X_ = np.linspace(x.min(), x.max(), 500)
Y_ = X_Y_Spline(X_)


plt.plot(X_, Y_)
plt.title("Plot Smooth Curve Using the scipy.interpolate.make_interp_spline() Class")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Output:


download
Smooth Curve

Example 2: Spline Curve Using Cubic Interpolation

It generates a cubic interpolation curve using the scipy.interpolate.interp1d class and then we use the curve to determine the y-values for closely spaced x-values for a smooth curve. Here also we will be using np.linspace() method which returns evenly spaced samples, calculated over a specified interval. Let's take 500 equally spaced samples between min and max values along the X-axis to plot the curve. Depending on how curved you want the line to be, you can modify the value for the third(num) parameter.

Syntax:

numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None, axis = 0)

X_Y_Spline = scipy.interpolate.make_interp_spline(x, y)

Parameters:

  • start, stop: Range of values for which samples are needed.
  • num: Number of samples. Higher values create smoother curves.
  • endpoint: If True, includes the stop value.
  • kind: Type of interpolation. "cubic" fits a cubic spline curve.
  • x, y: Input data points used for interpolation.

Returns:

  • np.linspace() returns a NumPy array of evenly spaced values.
  • interp1d() returns an interpolation function which can be used to compute y-values for any x within the original range.

Example:

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

# Dataset
x=np.array([1, 2, 3, 4, 5, 6, 7, 8])
y=np.array([20, 30, 5, 12, 39, 48, 50, 3])

cubic_interpolation_model = interp1d(x, y, kind = "cubic")

# Plotting the Graph
X_=np.linspace(x.min(), x.max(), 500)
Y_=cubic_interpolation_model(X_)

plt.plot(X_, Y_)
plt.title("Plot Smooth Curve Using the scipy.interpolate.interp1d Class")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Output: 

download
Smooth Curve

Next Article

Similar Reads