ขั้นตอนการทำงาน
- กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
- สร้าง DataFrame ของ Pandas พร้อมคีย์ straight และ square
- สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
- เพิ่ม subplot ลงใน figure ด้วย nrows=1, cols=2, index=1
- พล็อตจุดข้อมูล DataFrame ด้วย scatter()
- เพิ่ม subplot ด้วย nrows=1, cols=2, index=2
- กำหนดตัวแปร font_size, bbox สำหรับตาราง
- ปิด axes ปัจจุบัน
- เพิ่มตารางลงใน axes ปัจจุบันด้วย table()
- ใช้เมธอด show() เพื่อแสดงรูปภาพ
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'Straight': [i for i in range(10)], 'Square': [i * i for i in range(10)]})
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.scatter(x=df.Straight, y=df.Square)
ax2 = fig.add_subplot(122)
font_size = 14
bbox = [0, 0, 1, 1]
ax2.axis('off')
mpl_table = ax2.table(cellText=df.values, rowLabels=df.index, bbox=bbox, colLabels=df.columns)
plt.show()
ผลลัพธ์
