Visualisasi di Python

Pengantar Python untuk Keuangan

Adina Howe

Instructor

Matplotlib: paket visualisasi

Lihat galeri Matplotlib selengkapnya lewat tautan ini.

matplotlib

Pengantar Python untuk Keuangan

matplotlib.pyplot - beragam fungsi plotting

import matplotlib.pyplot as plt
Pengantar Python untuk Keuangan

matplotlib.pyplot - beragam fungsi plotting

  • plt.plot()

    • menerima argumen yang menjelaskan data untuk diplot
  • plt.show()

    • menampilkan plot ke layar
Pengantar Python untuk Keuangan

Plot dengan pyplot

import matplotlib.pyplot as plt
plt.plot(months, prices)
plt.show()
Pengantar Python untuk Keuangan

Hasil plot

Plot sederhana2

Pengantar Python untuk Keuangan

Garis merah solid

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red')
plt.show()
Pengantar Python untuk Keuangan

Hasil plot

garis merah

Pengantar Python untuk Keuangan

Garis putus-putus

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
Pengantar Python untuk Keuangan

Hasil plot

putus-putus

Pengantar Python untuk Keuangan

Warna dan gaya garis

warna
'green' hijau
'red' merah
'cyan' sian
'blue' biru
gaya garis
'-' solid
'--' putus-putus
'-.' putus-titik
':' titik-titik

Dokumentasi warna dan garis selengkapnya di sini.

Pengantar Python untuk Keuangan

Menambahkan label dan judul

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# Add labels
plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')

# Show plot
plt.show()
Pengantar Python untuk Keuangan

Hasil plot

plot berlabel

Pengantar Python untuk Keuangan

Menambahkan garis lain

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# adding an additional line
plt.plot(months, prices_new, color = 'green', linestyle = '--') 

plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')
plt.show()
Pengantar Python untuk Keuangan

Hasil plot

dua garis

Pengantar Python untuk Keuangan

Scatter plot

import matplotlib.pyplot as plt
plt.scatter(x = months, y = prices, color = 'red')
plt.show()
Pengantar Python untuk Keuangan

Hasil scatter plot

scatter plot

Pengantar Python untuk Keuangan

Ayo berlatih!

Pengantar Python untuk Keuangan

Preparing Video For Download...