การเพิ่ม Legend และ Title ให้ Grouped Histograms
ใช้ df.plot(kind='hist') แล้วเพิ่ม title ด้วย plt.title()
ขั้นตอนหลัก
- ตั้งค่าขนาดรูปและ padding
- สร้าง DataFrame พร้อมคอลัมน์ a, b, c, d
- พล็อตด้วย df.plot(kind='hist')
- ตั้งค่า title ด้วย plt.title()
- แสดงผลด้วย show()
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'a': [1, 1, 1, 1, 3],
'b': [1, 1, 2, 1, 3],
'c': [2, 2, 2, 1, 3],
'd': [2, 1, 2, 1, 3]})
df.plot(kind='hist')
plt.title("Grouped Histograms")
plt.show()
คำอธิบาย
Pandas plot kind='hist' จะสร้าง overlapping histograms สำหรับแต่ละคอลัมน์ legend จะสร้างอัตโนมัติจาก column names