เพื่อสร้าง Legend ด้วย Pandas และ matplotlib.pyplot() สามารถทำได้ดังนี้
- ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
- สร้าง DataFrame 2 มิติ
- พลอต DataFrame ด้วย kind='bar' และ legend=True
- แสดงรูปด้วย show()
ตัวอย่าง
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
df = pd.DataFrame({'Numbers': [3, 4, 1, 7, 8, 5], 'Frequency': [2, 4, 1, 4, 3, 2]})
df.plot(ax=ax, kind='bar', legend=True)
plt.show()ผลลัพธ์
