Label dan Judul Matplotlib
Buat Label untuk Plot
Dengan Pyplot, Anda dapat menggunakan fungsi xlabel()
and
ylabel()
untuk menyetel label untuk sumbu x dan y.
Contoh
Tambahkan label ke sumbu x dan y:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Hasil:
Buat Judul untuk Plot
Dengan Pyplot, Anda dapat menggunakan title()
fungsi untuk menetapkan judul plot.
Contoh
Tambahkan judul plot dan label untuk sumbu x dan y:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Hasil:
Atur Properti Font untuk Judul dan Label
Anda dapat menggunakan fontdict
parameter di
xlabel()
, ylabel()
, dan title()
untuk mengatur properti font untuk judul dan label.
Contoh
Atur properti font untuk judul dan label:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports
Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict =
font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.plot(x,
y)
plt.show()
Hasil:
Posisikan Judul
Anda dapat menggunakan loc
parameter in
title()
untuk memposisikan judul.
Nilai hukumnya adalah: 'kiri', 'kanan', dan 'tengah'. Nilai default adalah 'pusat'.
Contoh
Posisikan judul di sebelah kiri:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data", loc = 'left')
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.show()