Cara membuat grafik sinus di python

Presentasi berjudul: "Plotting dengan Python"— Transcript presentasi:

1 Plotting dengan Python
Program Digitalent Kominfo: Big Data Analytics Disusun oleh: Astria Nur Irfansyah, PhD 2018

2 Matplotlib Library Matplotlib boleh jadi adalah standar de facto untuk visualisasi 2 dimensi di Python. Ada beberapa cara penggunaannya: Pendekatan mirip MATLAB: dengan modul pyplot. Pendekatan object-oriented: dengan axes.Axes dan figure.Figure.

3 Program pyplot pertama
import matplotlib.pyplot as plt plt.plot([1, 2, 3, 1, 2.5, 4]) plt.ylabel('Sembarang bilangan') plt.show()

4 Program pyplot pertama cara ke-2
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3, 1, 3.5, 4]) ax.set(ylabel = 'Sembarang bilangan') plt.show() Pada contoh-contoh selanjutnya, cara ini akan digunakan.

5 Plot garis (x, y) import matplotlib.pyplot as plt x = [1, 2, 4, 5, 10] y = [-1, 9, 3, 4, 8] fig, ax = plt.subplots() ax.plot(x, y, 'ro-')# garis dan bulatan ax.set(ylabel = 'sumbu y', xlabel ='sumbu x') plt.show()

6 Hasil plot garis (x,y)

7 Komponen-komponen figure

8 Plot garis (x, y) lanjut import matplotlib.pyplot as plt x = [1, 2, 4, 5, 10] y = [-1, 9, 3, 4, 8] fig, ax = plt.subplots() ax.plot(x, y, 'ro-')# garis dan bulatan ax.set(ylabel = 'sumbu y', xlabel ='sumbu x’) plt.show()

9 Plot garis (x,y) lebih lanjut
import matplotlib.pyplot as plt # data pasangan koordinat (x,y) x = [1, 2, 4, 5, 10] y = [-1, 9, 3, 4, 8] fig, ax = plt.subplots() # plot berupa garis dan bulatan merah ax.plot(x, y, 'ro-') # label pada sumbu x dan y ax.set(ylabel = 'sumbu y', xlabel ='sumbu x') # range untuk sumbu x dan y ax.set(xlim = (0,10), ylim = (-2,10)) # ticks pada sumbu x dan y ax.set(xticks = list(range(0,10)), yticks = [-1, 0, 1, 2, 3, 4, 6, 8]) # judul gambar ax.set(title = 'Gambar sederhana') # garis grid on ax.grid() plt.show()

10 Hasil plot garis (x,y) lanjut

11 Numpy dan Matplotlib Secara prinsip, menyerupai cara plot di MATLAB
import matplotlib import matplotlib.pyplot as plt import numpy as np # Data for plotting t = np.arange(0.0, 2.0, 0.01) s1 = 1 + np.sin(2 * np.pi * t)# sinus s2 = *t # garis lurus fig, ax = plt.subplots() ax.plot(t, s1, 'r-', t, s2, 'b:') ax.set(xlabel='Waktu t (s)', ylabel='Tegangan (mV)', title='Plot dua fungsi') ax.grid() ax.legend(['s1 = 1 + sin(2*pi*t)', 's2= *t']) fig.savefig("test.png") plt.show() Secara prinsip, menyerupai cara plot di MATLAB

12 Hasil plot

13 Membuat dua plot bersebelahan
import matplotlib import matplotlib.pyplot as plt import numpy as np # Data for plotting t = np.arange(0.0, 2.0, 0.01) s1 = 1 + np.sin(2 * np.pi * t)# sinus s2 = *t # garis lurus # buat plot 2 baris 1 kolom, yang pertama ax1 = plt.subplot(211) ax1.plot(t, s1, 'r-') # buat plot 2 baris 1 kolom, yang kedua ax2 = plt.subplot(212, sharex = ax1) ax2.plot(t, s2, 'b:') ax1.set(ylabel = "S1 (mV)") ax2.set(xlabel = "Waktu t(s)", ylabel = "S2 (mV)") ax1.set(title = "Plot dua fungsi") plt.show()

14 Membuat dua plot bersebelahan ( versi 2)
import matplotlib import matplotlib.pyplot as plt import numpy as np # Data for plotting t = np.arange(0.0, 2.0, 0.01) s1 = 1 + np.sin(2 * np.pi * t)# sinus s2 = *t # garis lurus # buat plot 2 baris 1 kolom fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex = True) ax1.plot(t, s1, 'r-') ax2.plot(t, s2, 'b:') ax1.set(ylabel = "S1 (mV)") ax2.set(xlabel = "Waktu t(s)", ylabel = "S2 (mV)") ax1.set(title = "Plot dua fungsi") plt.show()

15 Hasil dua plot bersebelahan

16 Visualisasi data dengan Pandas
Di Pandas, dataframe memiliki function plot. import pandas as pd import matplotlib.pyplot as plt # Baca hasil survei kelas Jumat sore # di data = pd.read_csv(' sep = ';') # ambil data jarak 20 peserta ke ITS (dalam kilometer) data[0:20].plot(kind = 'barh', y="jarak") plt.show() Plot di Pandas, ada pilihan “kind”

17 Hasil plot dengan Pandas

18 Mengatur plot Pandas dengan Matplotlib
import pandas as pd import matplotlib.pyplot as plt # Baca hasil survei kelas Jumat sore # di data = pd.read_csv(' sep = ';') # Menggunakan style bernama classic plt.style.use('classic') # Dapatkan fig dan ax fig, ax = plt.subplots() # plot data jarak 20 peserta ke ITS (dalam km) data[0:20].plot(kind='barh',y="jarak",ax=ax) # hitung mean, lalu tampilkan garis vertikalnya avg = data[0:20]['jarak'].mean() ax.axvline(x=avg, color='b', label='Rerata', linestyle='--', linewidth=1) # Atur tampilan ax.set(xlabel = "Jarak (km)", ylabel = "Nomor peserta", title="Hasil survei") ax.grid() plt.show()

19 Hasil mengatur plot Pandas dengan Matplotlib

20 Pilihan “kind” pada pandas.DataFrame.plot
kind : str ‘line’ : line plot (default) ‘bar’ : vertical bar plot ‘barh’ : horizontal bar plot ‘hist’ : histogram ‘box’ : boxplot ‘kde’ : Kernel Density Estimation plot ‘density’ : same as ‘kde’ ‘area’ : area plot ‘pie’ : pie plot ‘scatter’ : scatter plot ‘hexbin’ : hexbin plot

21 Tugas Cari file CSV apapun yang dapat digunakan sebagai file input
Gunakan pandas untuk meload file tersebut. Tampilkan dataframe tersebut dengan berbagai tipe (kind) tampilan: Vertical bar Pie plot Histogram Box plot Gunakan Matplotlib untuk menambahkan pengaturan tampilan seperlunya (label, legend, title, dll)