Home » python » Matplotlib Subplot: Code dan Cara Membuatnya

Matplotlib Subplot: Code dan Cara Membuatnya

by Catur Kurnia Sari
by Catur Kurnia Sari

Tampilkan Banyak Plot

Dengan fungsi subplots() kita bisa menggambar banyak plot dalam satu gambar.

Contoh:
Gambar 2 plot

#Tiga baris agar kompiler kita bisa menggambar:
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()

#Dua baris agar kompiler kita bisa menggambar:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Hasilnya:

Fungsi subplots()

Fungsi subplots() mengambil tiga argumen yang menjelaskan layout gambar.

Layout diatur dalam baris dan kolom, yang diwakili oleh argumen pertama dan kedua.

Argumen ketiga mewakili indeks plot saat ini.

plt.subplot(1, 2, 1)
#gambar tersebut memiliki 1 baris, 2 kolom, dan plot ini adalah plot pertama.
plt.subplot(1, 2, 2)
#gambar tersebut memiliki 1 baris, 2 kolom, dan plot ini adalah plot kedua.

Jadi, jika kita ingin membuat gambar dengan 2 baris 1 kolom maka kedua plot akan ditampilkan di atas satu sama lain, bukan berdampingan, kita dapat menulis sintaks seperti ini.

Contoh:
Gambar 2 plot di atas satu sama lain

#Tiga baris agar kompiler kita bisa menggambar:
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()

#Dua baris agar kompiler kita bisa menggambar:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Hasilnya:

Kita dapat menggambar plot sebanyak yang kita inginkan pada satu gambar, cukup tentukan jumlah baris, kolom, dan indeks plot.

Contoh:
Gambar 6 plot

#Tiga baris agar kompiler kita bisa menggambar:
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 2)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 4)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 6)
plt.plot(x,y)

plt.show()

#Dua baris agar kompiler kita bisa menggambar:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Judul

Kita dapat menambahkan judul ke setiap plot dengan fungsi title().

Contoh:
2 plot, dengan judul

#Tiga baris agar kompiler kita bisa menggambar:
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.show()

#Dua baris agar kompiler kita bisa menggambar:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Super Title

Kita bisa menambahkan judul ke seluruh gambar dengan fungsi suptitle()

Contoh:
Tambahkan judul untuk keseluruhan gambar

#Tiga baris agar kompiler kita bisa menggambar:
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.suptitle("MY SHOP")
plt.show()

#Dua baris agar kompiler kita bisa menggambar:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

You may also like