Matplotlib adalah salah satu library yang paling populer untuk membuat plot dan visualisasi data dalam Python. Salah satu fungsi utama dalam library ini adalah matplotllib.pyplot.scatter() yang digunakan untuk membuat scatter plot, yaitu plot yang menghubungkan setiap titik koordinat x dan y dengan titik warna yang berbeda.

Contoh 1: Scatter Plot Dasar

Berikut adalah contoh dasar penggunaan fungsi matplotlib.pyplot.scatter() untuk membuat scatter plot:

import matplotlib.pyplot as plt

x = [5, 7, 8, 7, 2, 17, 2, 9,
 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86,
 103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y, c="blue")
plt.show()

Output:

Basic Scatter Plot

Contoh 2: Scatter Plot dengan Datasets Berbeda

Berikut adalah contoh penggunaan fungsi matplotlib.pyplot.scatter() untuk membuat scatter plot dengan dua datasets berbeda:

import matplotlib.pyplot as plt

x1 = [89, 43, 36, 36, 95, 10,
 66, 34, 38, 20]
y1 = [21, 46, 3, 35, 67, 95,
 53, 72, 58, 10]

x2 = [26, 29, 48, 64, 6, 5,
 36, 66, 72, 40]
y2 = [26, 34, 90, 33, 38,
 20, 56, 2, 47, 15]

plt.scatter(x1, y1, c="pink", 
 linewidths=2, 
 marker="s", 
 edgecolor="green", 
 s=50)

plt.scatter(x2, y2, c="yellow",
 linewidths=2,
 marker="^", 
 edgecolor="red", 
 s=200)

plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output:

Scatter Plot with Multiple Datasets

Contoh 3: Bubble Chart

Berikut adalah contoh penggunaan fungsi matplotlib.pyplot.scatter() untuk membuat bubble chart:

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [2, 3, 5, 7, 11]
bubble_sizes = [30, 80, 150, 200, 300]

plt.scatter(x_values, y_values, s=bubble_sizes, alpha=0.6, edgecolors='b', linewidths=2)

plt.title("Bubble Chart with Transparency")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.show()

Output:

Bubble Chart

Contoh 4: Scatter Plot Terkustomisasi

Berikut adalah contoh penggunaan fungsi matplotlib.pyplot.scatter() untuk membuat scatter plot tercustomisasi menggunakan NumPy:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 100 * np.random.rand(50)

plt.scatter(x, y, c=colors, s=sizes, alpha=0.7, cmap='viridis')

plt.title("Customized Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.colorbar(label='Color Intensity')

plt.show()

Output:

Customized Scatter Plot

Konklusi

Dalam kesimpulan, matplotllib.pyplot.scatter() dalam Python adalah alat yang sangat fleksibel dan kuat untuk visualisasi relasi antara variabel melalui scatter plot. Fleksibilitasnya memungkinkan untuk customisasi marker, warna, ukuran, dan properti lainnya, sehingga memberikan cara dinamis untuk merepresentasikan pola data kompleks. Dalam penggunaan basic exploratory analysis atau interpretasi data, matplotllib.pyplot.scatter() adalah salah satu fungsi yang paling populer dalam library Matplotlib.