3/31/2020 DataVis1 - Jupyter Notebook
Contoh menampilkan grafik menggunakan
Python
In [3]: 1 # Mengimport library matplotlib
2 import matplotlib
In [2]: 1 #Import pyplot
2 from matplotlib import pyplot as plt
3
4 #Tentukan data yang akan ditampilkan ke grafik
5 plt.plot([1,2,3,4,5,6,7,8,9],[4,5,1,2,5,2,4,6,7])
6
7 #Tampilkan data berikut ke layar dengan menekan tombol <Run> sehingga dip
8 plt.show()
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 1/7
3/31/2020 DataVis1 - Jupyter Notebook
In [4]: 1 #Dengan script berikut, kita tambah infomasi axis x dan y
2 from matplotlib import pyplot as plt
3
4 x = [1,2,3,4,5,6,7,8,9]
5 y = [4,5,1,2,5,2,4,6,7]
6
7 plt.plot(x,y)
8
9 plt.title('Judul Grafik')
10 plt.ylabel('sumbu Y')
11 plt.xlabel('sumbu X')
12
13 plt.show()
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 2/7
3/31/2020 DataVis1 - Jupyter Notebook
In [8]: 1 from matplotlib import pyplot as plt
2 from matplotlib import style
3
4 #Ini style untuk merubah tampilan grafiknya
5 style.use('ggplot')
6
7 x = [1,3,4,6,8,9,13,14,15]
8 y = [4,5,1,2,5,2,4,6,7]
9
10 x2 = [2,3,5,7,8,10,11,12,16]
11 y2 = [6,15,7,3,2,8,12,13,7]
12
13 # parameter linewidth untuk merubah ketebalan garis
14 plt.plot(x,y,linewidth=2)
15 plt.plot(x2,y2,linewidth=4)
16
17 plt.title('Judul Grafik')
18 plt.ylabel('sumbu Y')
19 plt.xlabel('sumbu X')
20
21 plt.show()
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 3/7
3/31/2020 DataVis1 - Jupyter Notebook
In [18]: 1 from matplotlib import pyplot as plt
2 from matplotlib import style
3
4 #Ini style untuk merubah tampilan grafiknya
5 style.use('ggplot')
6
7 x = [1,3,4,6,8,9,13,14,15]
8 y = [4,5,1,2,5,2,4,6,7]
9
10 x2 = [2,3,5,7,8,10,11,12,16]
11 y2 = [6,15,7,3,2,8,12,13,7]
12
13 # parameter linewidth untuk merubah ketebalan garis
14 plt.plot(x,y,'g',label='line hijau',linewidth=2)
15 plt.plot(x2,y2,'c',label='line cyan',linewidth=4)
16
17 #menampilkan legend
18 plt.legend()
19
20 plt.title('Grafik Garis')
21 plt.ylabel('sumbu Y')
22 plt.xlabel('sumbu X')
23
24 #Jika perintah berikut di-uncomment, maka grid muncul di layar
25 #plt.grid(True,color='k')
26
27 plt.show()
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 4/7
3/31/2020 DataVis1 - Jupyter Notebook
In [19]: 1 from matplotlib import pyplot as plt
2 from matplotlib import style
3
4 #Ini style untuk merubah tampilan grafiknya
5 style.use('ggplot')
6
7 x = [1,4,6,8,9,13,14,15]
8 y = [4,5,1,5,2,4,6,7]
9
10 x2 = [2,3,5,7,10,11,12,16]
11 y2 = [6,15,7,3,8,12,13,7]
12
13 # parameter linewidth untuk merubah ketebalan garis
14 #plt.plot(x,y,'g',label='line hijau',linewidth=2)
15 #plt.plot(x2,y2,'c',label='line cyan',linewidth=4)
16
17 # parameter grafik batang
18 plt.bar(x, y, align='center')
19 plt.bar(x2, y2, color='g', align='center')
20
21 #menampilkan legend
22 #plt.legend()
23
24 plt.title('Grafik Batang')
25 plt.ylabel('sumbu Y')
26 plt.xlabel('sumbu X')
27
28 plt.show()
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 5/7
3/31/2020 DataVis1 - Jupyter Notebook
In [4]: 1 from matplotlib import pyplot as plt
2 from matplotlib import style
3
4 #Ini style untuk merubah tampilan grafiknya
5 style.use('ggplot')
6
7 x = [1,4,6,8,9,13,14,15]
8 y = [4,5,1,5,2,4,6,7]
9
10 x2 = [2,3,5,7,10,11,12,16]
11 y2 = [6,15,7,3,8,12,13,7]
12
13 # parameter linewidth untuk merubah ketebalan garis
14 #plt.plot(x,y,'g',label='line hijau',linewidth=2)
15 #plt.plot(x2,y2,'c',label='line cyan',linewidth=4)
16
17 # parameter scatter
18 plt.scatter(x, y)#, align='center')
19 plt.scatter(x2, y2, color='g')#, align='center')
20
21 #menampilkan legend
22 #plt.legend()
23
24 plt.title('Grafik Skater (Tebar)')
25 plt.ylabel('sumbu Y')
26 plt.xlabel('sumbu X')
27
28 plt.show()
Diagram scatter (tebar) umumnya digunakan untuk menunjukkan hubungan antara dua variable
numerik dalam satu grafik. Diagram jenis ini sangat berguna terutama untuk analisis regresi,
karena membantu kita mendeteksi pola.
In [ ]: 1
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 6/7
3/31/2020 DataVis1 - Jupyter Notebook
localhost:8888/notebooks/ProbStat/DataVis1.ipynb 7/7