Menghasilkan Grafik Kurva yang Halus dengan Matplotlib

Menghasilkan Grafik Kurva yang Halus dengan Matplotlib

Banyak kali kita memiliki plot garis yang dihasilkan dari daftar data yang tergantung, sehingga grafik tampak seperti garis lurus yang menghubungkan titik-titik atau sangat padat, sehingga titik data menjadi sangat dekat satu sama lain dan plotnya menjadi penuh.

Fungsi matplotlib.pyplot.plot() secara default memproduksi kurva dengan menjalin dua titik berdekatan dalam data dengan garis lurus, sehingga fungsi matplotlib.pyplot.plot() tidak memproduksi kurva yang halus untuk rentang data kecil.

Contoh:

Python

import numpy as np
import matplotlib.pyplot as plt


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

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

Output:

Observe that the plot is not at all smooth since the underlying data doesn’t follow a smooth line. To plot a smooth curve, we first fit a spline curve to the curve and use the curve to find the y-values for x values separated by an infinitesimally small gap. We can get a smooth curve by plotting those points with a very infinitesimally small gap.

Kita dapat menggunakan metode berikut untuk menciptakan kurva yang halus untuk dataset ini:

  1. Smooth Spline Curve dengan PyPlot
    It plots a smooth spline curve by first determining the spline curve’s coefficients using the scipy.interpolate.make_interp_spline(). We use the given data points to estimate the coefficients for the spline curve, and then we use the coefficients to determine the y-values for very closely spaced x-values to make the curve appear smooth. Here we will be using np.linspace() method which returns evenly spaced samples, calculated over a specified interval. Optional parameter num is the number of samples to generate within the range start and stop. The default value is 50 and must be non-negative. We want this parameter to be of a sufficiently high value to generate a smooth curve. Let’s take 500 equally spaced samples between min and max values along the X-axis to plot the curve.

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)

Contoh:

Python

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


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)



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:

  1. Spline Curve menggunakan Interpolasi Kubik
    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)
cubic_interpolation_model=scipy.interpolate.interp1d(x,y,kind="cubic")

Contoh:

Python

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


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")


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()

Dengan menggunakan metode-metode di atas, kita dapat dengan mudah menghasilkan grafik kurva yang halus dengan menggunakan library Matplotlib.

Leave a comment