Anda di halaman 1dari 21

LAPORAN PRAKTIKUM PROGRAM PENGOLAHAN DATA STATISTIKA

MATPLOTLIB

Disusun Oleh :
Farhan Nurhidayah (J0303201015)
A1
Teknologi Rekayasa Perangkat Lunak

SEKOLAH VOKASI
INSTITUT PERTANIAN BOGOR
2022
1. Source code:
import matplotlib.pyplot as plt

y = [2990, 2710, 2540, 3300, 2990]


x = [18, 19, 20, 21, 22]

plt.plot(x, y)

plt.xlabel('Tanggal (Januari)')
plt. ylabel('Harga')

plt.title('Harga Emiten ANTM')


plt.grid(True)

plt.show()

Output:
Insight:
Pada tanggal 18 – 20 harga emiten mengalami penurunan ketitik terendah dengan harga dibawah
2600, lalu setelah itu harga emiten naikkembali ketitik tertinggi dengan harga 3300 kemudian kembali
mengalami penurunan.
2. Source code:

import numpy as np

import matplotlib.pyplot as plt

t = np.arange(0.0, 3.0, 0.01)


s = np.sin(2.5 * np.pi * t)

plt.plot(t, s)

plt.xlabel('time(s)')
plt.ylabel('voltage(V)')

plt.title('Gelombang Sinus')
plt.grid(True)

plt.show()

Output:
Insight:

Gelombang sinus diatas mengalami fluktuasi setiap 0.5 detik.


3. Source code:
import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_axes([0, 0, 1, 1])
Nilai = [80, 90, 65, 79, 82]
ax.plot(Nilai)

plt.show()

Output:

Insight:

Gambar diatas menggambarkan sebuah grafik nilai yang mengalami fluktuasi.


4. Source code:
from matplotlib import pyplot as plt
from matplotlib import style

style.use('ggplot')

x = [0, 1, 2, 3, 4 , 5]
y = [24.27, 23.18, 22.39, 8.41, 7.19, 6.62]

fig, ax = plt.subplots()

ax.bar(x, y, align='center')

ax.set_title('Bahasa Pemrograman Favorit (UMSU)')


ax.set_ylabel('Pengguna')
ax.set_xlabel('Bahasa')

ax.set_xticks(x)
ax.set_xticklabels(("Python", "Javascript", "Java","C#", "PHP","C"))

plt.show()

Output:
Insight:
Gambar diatas merupakan gambar histogram mengenai bahasa pemrograman yang paling
banyak digunakan. Bahasa pemrograman python merupakan bahasa pemrograman yang paling
banyak digunakan yaitu berkisar 24% pengguna, disusul Javascript dengan 23% pengguna, Java
22% pengguna, C# 8% pengguna, PHP 7% pengguna dan C 6.5% pengguna.

5. Source code:
import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_axes([0, 0, 1, 1])

MataKuliah = ['Matematika', 'Fisika', 'Kimia', 'Komputer', 'Bahasa']

Nilai = [80, 90, 65, 79, 82]

ax.bar(MataKuliah, Nilai)

plt.show()

Output:
Insight:
Gambar diatas merupakan gambar histogram mengenai nilai dari beberapa mata kuliah. Nilai
tertinggi adalah nilai fisika dengan nilai 90, disusul nilai bahasa dengan nilai 82, matematika
dengan nilai 80, komputer dengan nilai 79 dan terakhir nilai kimia dengan nilai 65.

6. Source code:
import matplotlib.pyplot as plt

from Latihan5 import MataKuliah

fig = plt.figure()

ax = fig.add_axes([0, 0, 1, 1])

ax.axis('equal')
MataKuliah = ['Matematika', 'Fisika', 'Kimia', 'Komputer', 'Bahasa']
Nilai = [80, 90, 65, 79, 82]

ax.pie(Nilai, labels=MataKuliah, autopct='%1.2f%%')

plt.show()

Output:
Insight:
Gambar diatas merupakan gambar pie chart mengenai nilai dari beberapa mata kuliah dalam
bentuk persentase. Persentase nilai tertinggi adalah nilai fisika dengan persentase 22.73% dan
yang terendah adalah nilai kimia dengan persentase 16.41%.

7. Source code:
import matplotlib.pyplot as plt

labels = ['Dokter', 'Guru', 'Pegawai Kantor', 'Pedagang']

quantity = [3, 10, 12, 11]

colors = ['yellowgreen', 'green', 'lightskyblue', 'lightcoral']

plt.title('Daftar Pekerjaan Orang Tua Siswa Kelas XII-A')


plt.pie(quantity, labels=labels, colors=colors, autopct='%1.1f%%',
shadow=True, startangle=90)

plt.axis('equal')

plt.show()

Output:
Insight:
Gambar diatas merupakan gambar pie chart mengenai daftar pekerjaan orang tua di kelas XII-A.
Daftar pekerjaan orang tua yang paling banyak adalah pegawai kantor dengan persentase 33.3%
dan yang terkecil adalah dokter dengan persentase 8.3%.

8. Source code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
tugas = [80, 90, 65, 79, 82]
uts = [70, 80, 85, 59, 92]
uas = [82, 92, 87, 77, 84]

ax.scatter(tugas, uts, color='red')

ax.scatter(tugas,uas, color='blue')

ax.set_xlabel('Tugas')
ax.set_ylabel('Nilai')
ax.set_title('Scatter Plot')

plt.show()
Output:

Insight:
Gambar diatas merupakan hasil dari plotting titik data antar sumbu x (Tugas) dan sumbu y (nilai)
untuk mencari korelasi antar atribut. Scatter plot menggunkan titik-titik untuk mewakili nilai uts
dan uas yakni variabel numerik yang berbeda. Dimana scatter merah adalah uts dan biru uas

9. Source code:
from cmath import pi
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 2*pi, 0.1)


y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1)
plt.plot(x, y1, color='blue', linewidth=2.0, linestyle="-",
label='$sin(x)')
plt.plot(x, y2, color='green',linewidth=2.0, linestyle="--",
label='$cos(x)' )
plt.xlabel('x')
plt.ylabel('y')

plt.show()
Output:

Insight:

Gambar diatas merupakan grafik mengenai nilai sin dan cos. Garis berwarna biru adalah nilai sin
dan garis berwarna merah adalah nilai cos. Terdapat fluktuasi dari kedua nilai tersebut.
10. Source Code:

import matplotlib.pyplot as plt

import numpy as np

X = np.array([5, 15, 25, 35, 45, 55])


Y = np.array([5, 20, 14, 32, 22, 38])

plt.scatter(X,Y)

plt.show()

Output:

Insight:

Gambar diatas merupakan hasil dari plotting titik data antar sumbu x dan sumbu y untuk mencari
korelasi antar atribut. Scatter plot menggunkan titik-titik untuk mewakili nilai untuk dua variabel
numerik yang berbeda.
11. Source Code:

import matplotlib.pyplot as plt

import numpy as np

X = np.array([5, 15, 25, 35, 45, 55])


Y = np.array([5, 20, 14, 32, 22, 38])

plt.scatter(X,Y)

Z = np.polyfit(X, Y, 1)
P = np.poly1d(Z)

plt.plot(X, P(X), 'r')


plt.show()

Output:

Insight :

Gambar diatas merupakan gambar scatter plot, yaitu sebaran titik-titik data yang mana
ketika sebaran titik-titik data itu berdekatan maka hubungan variabel x dengan variabel y
memiliki hubungan korelasi yang cukup kuat, linier dan bernilai positif (nilai x dan y
mengarah ke atas).
12. Source Code:
import matplotlib.pyplot as plt

litho = ('SHALE', 'SILT', 'SAND', 'LIMESTONE')


persen = [40, 20, 30, 10]
warna = ['green', 'grey', 'orange', 'blue']
expl = (0, 0, 0.1 , 0)

plt.pie(persen,explode=expl, labels=litho, colors=warna,


autopct='%1.1f%%', shadow=True, counterclock=False, startangle=90)

plt.axis('equal')

plt.show()

Output:

Insight:

Berikutnya adalah Diagram pie diatas menggambar satu bagian untuk setiap nilai yaitu [40, 20,
30, 10]. Diagram pie diatas pun Menambahkan parameter explode di satu irisan agar nilai sand
(30) lebih menonjol dan memberikan efek bayangan dibelakang diagram yakni menggunakan
fungsi parameter shadow.
13. Source Code:
import matplotlib.pyplot as plt

litho = ('SHALE', 'SILT', 'SAND', 'LIMESTONE')


persen = [40, 20, 30, 10]
warna = ['green', 'grey', 'orange', 'blue']
pos = (1, 2, 3, 4)
plt.bar(pos, persen, color=warna, tick_label=litho)

plt.show()

Output:

Insight:

Gambar diatas adalah diagram batang yang memiliki nilai [40,20,30,10] dengan angka
presentase tertinggi dari nilai Shale.
14. Source Code:
import matplotlib.pyplot as plt
import numpy as np

litho = ('SHALE', 'SILT', 'SAND', 'LIMESTONE')


persen = [40, 20, 30, 10]
warna = ['green', 'grey', 'orange', 'blue']
pos = (1, 2, 3, 4)
x = np.arange(0, 2*np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure(figsize=(10, 8))


ax1 = fig.add_subplot(2, 2, 1)
plt.plot(x, y1, color='blue', linewidth=2.0, linestyle="-",
label='$sin(x)')
ax2 = fig.add_subplot(2, 2, 2)
plt.plot(x, y2, color='green',linewidth=2.0, linestyle="--",
label='$cos(x)' )
ax3 = fig.add_subplot(2, 1, 2)
ax3.bar(pos, persen, color=warna, tick_label=litho)

plt.show()

Output:
Insight:
Gambar diatas adalah contoh beberapa chart yang digabungkan dalam 1 plot Chart tersebut
adalah subplot 221 dan 222 dan yang dibawah adalah 212

15. Source Code:


import matplotlib.pyplot as plt
import numpy as np

litho = ('SHALE', 'SILT', 'SAND', 'LIMESTONE')


persen = [40, 20, 30, 10]
warna = ['green', 'grey', 'orange', 'blue']
pos = (1, 2, 3, 4)
expl = (0, 0, 0.1 , 0)
x = np.arange(0, 2*np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure(figsize=(10, 8))


ax1 = fig.add_subplot(2, 2, 1)
plt.plot(x, y1, color='blue', linewidth=2.0, linestyle="-",
label='$sin(x)')
ax2 = fig.add_subplot(2, 2, 2)
plt.plot(x, y2, color='green',linewidth=2.0, linestyle="--",
label='$cos(x)' )
ax3 = fig.add_subplot(2, 2, 3)
ax3.pie(persen,explode=expl, labels=litho, colors=warna,
autopct='%1.1f%%', shadow=True, counterclock=False, startangle=90)
ax4 = fig.add_subplot(2, 2, 4)
ax4.bar(pos, persen, color=warna, tick_label=litho)

plt.show()
Output:

Insight:
Gambar diatas adalah contoh beberapa chart yang digabungkan dalam 1 plot Chart tersebut
adalah subplot 221, 222, 223 dan yang dibawah adalah 224. Subplot 221 adalah gambar untuk
plot grafik nilai sinus, subplot 222 adalah gambar untuk plot grafik nilai kosinus dengan ditandai
garis putus-putus, subplot 223 adalah gambar pie chart dengan nilai persentase dari 4 item, dan
subplot 224 adalah gambar untuk histogram (bar plot) dengan nilai persentase dari 4 item.
16. Source Code:
import matplotlib.pyplot as plt
import numpy as np

litho = ('SHALE', 'SILT', 'SAND', 'LIMESTONE')


persen = [40, 20, 30, 10]
warna = ['green', 'grey', 'orange', 'blue']
pos = (1, 2, 3, 4)
expl = (0, 0, 0.1 , 0)
x = np.arange(0, 2*np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

fig, ax = plt.subplots(2,2, figsize=(10, 8))


ax[0][0].plot(x, y1)
ax[0][1].plot(x,y2)
ax[1][0].pie(persen,explode=expl, labels=litho, colors=warna,
autopct='%1.1f%%', shadow=True, counterclock=False, startangle=90)
ax[1][1].bar(pos, persen, color=warna, tick_label=litho)

plt.show()

Ouput:

Insight:
Gambar diatas adalah contoh beberapa chart yang digabungkan dalam 1 plot Chart tersebut
adalah subplot 221, 222, 223 dan yang dibawah adalah 224. Subplot 221 adalah gambar untuk
plot grafik nilai sinus, subplot 222 adalah gambar untuk plot grafik nilai, subplot 223 adalah
gambar pie chart dengan nilai persentase dari 4 item, dan subplot 224 adalah gambar untuk
histogram (bar plot) dengan nilai persentase dari 4 item.

17. Source Code:


import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0, 2*np.pi, 0.1)


x1 = np.sin(y)
x2 = np.cos(y)
x3 = np.tan(y)

fig = plt.figure(figsize=(6, 8))


ax1 = fig.add_axes([0.1, 0.1, 0.2, 0.5])
ax1.set_yticklabels([])
ax1.plot(x1, y, color='green')
ax2 = fig.add_axes([0.4, 0.1, 0.2, 0.5])
ax2.set_yticklabels([])
ax2.plot(x2, y, color='blue')
ax3 = fig.add_axes([0.6, 0.1, 0.2, 0.5])
ax3.set_yticklabels([])
ax3.plot(x3, y, color='red')

plt.show()
Ouput:

Insight:
Layout plot, dimana setiap subplot ditentukan posisi serta panjang lebarnya. formatnya adalah
add_axes(titik_xawal, titik_yawal, lebar, panjang).

Anda mungkin juga menyukai