Tutorial Data Visualization dengan Python Menggunakan Matplotlib dan Seaborn
Apa Itu Data Visualization?
Data Visualization adalah representasi grafis dari informasi dan data. Dengan menggunakan elemen visual seperti grafik, peta, dan diagram, visualisasi data memudahkan kita mengenali pola, tren, dan outlier dalam data.
Manfaat utama visualisasi data:
-
Memudahkan analisis dataset besar
-
Menyajikan data kompleks dengan cara sederhana
-
Membantu pengambilan keputusan berbasis data
-
Menemukan pola atau masalah tersembunyi
Visualisasi Data di Python
Python memiliki banyak library visualisasi data, antara lain:
-
Matplotlib
-
Seaborn
-
Plotly
-
Bokeh
-
Altair
Dalam artikel ini, fokus kita adalah dua yang paling umum digunakan: Matplotlib dan Seaborn.
Mengenal Matplotlib
Matplotlib adalah library visualisasi data paling dasar dan fleksibel di Python. Dengan Matplotlib, Anda bisa membuat berbagai jenis grafik seperti line chart, bar chart, histogram, scatter plot, hingga pie chart. Kelebihan Matplotlib antara lain:
-
Sangat customizable
-
Bisa digunakan untuk membuat grafik sederhana hingga kompleks
-
Cocok untuk embed grafik ke aplikasi berbasis GUI
Namun, Matplotlib terkadang membutuhkan banyak kode untuk menghasilkan grafik yang menarik.
Mengenal Seaborn
Seaborn dibangun di atas Matplotlib dan menyederhanakan banyak aspek pembuatan grafik. Seaborn lebih fokus pada visualisasi data statistik dan secara default menghasilkan grafik yang lebih aesthetic tanpa banyak pengaturan tambahan.
Kelebihan Seaborn:
-
Lebih mudah digunakan untuk analisis statistik
-
Style grafik lebih modern dan profesional
-
Mudah membuat heatmap, pairplot, boxplot, dan grafik statistik lainnya
Perbandingan Matplotlib dan Seaborn
|
Aspek |
Matplotlib |
Seaborn |
|
Flexibility |
Sangat tinggi |
Tinggi (lebih fokus pada statistik) |
|
Customization |
Manual, butuh banyak kode |
Tampilan default sudah aesthetic |
|
Ease of Use |
Butuh lebih banyak konfigurasi |
Lebih ringkas dan otomatis |
|
Cocok digunakan untuk |
Semua jenis grafik |
Statistical dan exploratory data analysis |
|
Integrasi |
Dapat digabung dengan Seaborn |
Memanfaatkan Matplotlib untuk customization lebih lanjut |
Cara Memulai Visualisasi Data dengan Matplotlib dan Seaborn
Step 1: Install Libraries
|
pip install matplotlib seaborn |
Step 2: Load Dataset
|
import pandas as pd # Load dataset url = "https://raw.githubusercontent.com/novrian6/dataanalysis-dataset/master/WHO-COVID-19-global-data.csv" df = pd.read_csv(url, delimiter=';') # Show first five rows print(df.head()) |
Step 3: Import Matplotlib and Seaborn
|
import matplotlib.pyplot as plt import seaborn as sns |
Step 4: Visualization Examples with Matplotlib
Line Plot
|
# Convert 'Date_reported' to datetime format df['Date_reported'] = pd.to_datetime(df['Date_reported'], format='%d/%m/%Y') df_country = df[df['Country'] == 'Italy'] plt.figure(figsize=(12, 6)) plt.plot(df_country['Date_reported'], df_country['Cumulative_cases'], marker='o', linestyle='-', color='blue', label='Cumulative Cases') plt.xlabel('Date') plt.ylabel('Cumulative Cases') plt.title('Cumulative COVID-19 Cases in Italy') plt.legend() plt.grid(True) plt.show() |
Bar Plot
|
# Plot total new cases by country top_countries = df.groupby('Country')['New_cases'].sum().nlargest(10) plt.figure(figsize=(14, 8)) top_countries.plot(kind='bar', color='skyblue') plt.xlabel('Country') plt.ylabel('New Cases') plt.title('Top 10 Countries by New COVID-19 Cases') plt.xticks(rotation=45) plt.show() |
Histogram
|
# Distribution of new cases plt.figure(figsize=(12, 6)) plt.hist(df['New_cases'], bins=30, color='blue', edgecolor='black', alpha=0.7) plt.xlabel('New Cases') plt.ylabel('Frequency') plt.title('Distribution of New COVID-19 Cases') plt.grid(True) plt.show() |
Scatter Plot
|
# Scatter plot of new cases vs. new deaths plt.figure(figsize=(12, 6)) plt.scatter(df['New_cases'], df['New_deaths'], alpha=0.5, color='red') plt.xlabel('New Cases') plt.ylabel('New Deaths') plt.title('Scatter Plot of New Cases vs New Deaths') plt.grid(True) plt.show() |
Step 5: Visualization Examples with Seaborn
Line Plot with Seaborn
|
# Load the 'flights' dataset df_flights = sns.load_dataset('flights')df_flights.head() # Line plot of year vs. passengers sns.relplot(x='year', y='passengers', data=df_flights, kind='line') plt.show() |
Bar Plot with Seaborn
|
plt.figure(figsize=(14, 8)) sns.barplot(x=top_countries.index, y=top_countries.values, palette='Blues_d') plt.title('Top 10 Countries by New COVID-19 Cases (Seaborn)') plt.xlabel('Country') plt.ylabel('New Cases') plt.xticks(rotation=45) plt.show() |
Histogram with Seaborn
|
plt.figure(figsize=(12, 6)) sns.histplot(df['New_cases'], bins=30, kde=True, color='green') plt.title('Distribution of New COVID-19 Cases (Seaborn)') plt.xlabel('New Cases') plt.ylabel('Frequency') plt.show() |
Scatter Plot with Seaborn
|
plt.figure(figsize=(12, 6)) sns.scatterplot(data=df, x='New_cases', y='New_deaths', alpha=0.6, color='purple') plt.title('Scatter Plot of New Cases vs New Deaths (Seaborn)') plt.xlabel('New Cases') plt.ylabel('New Deaths') plt.show() |
Kesimpulan
-
Matplotlib cocok untuk plot dasar dan customized.
-
Seaborn menyediakan visualisasi statistik yang lebih cepat dan menarik.
-
Kombinasi keduanya memungkinkan analisis data yang lebih mendalam.
Dengan menguasai Matplotlib dan Seaborn, Anda dapat membuat visualisasi data yang informatif dan menarik untuk mendukung pengambilan keputusan berbasis data.
Ingin Menguasai Data Visualization dengan Python?
Pelajari lebih dalam teknik visualisasi data menggunakan Matplotlib dan Seaborn langsung dari para ahli di SUHU!
Kini saatnya memperkuat tim Anda dengan mengikuti pelatihan dan sertifikasi di bidang Data Analyst di SUHU.
Berikut rekomendasi pelatihan untuk Anda atau tim IT Anda:
Silakan konsultasikan kebutuhan Anda bersama kami dengan klik link berikut: https://bit.ly/kontaksuhu
