เพื่อตั้งค่า opacity แตกต่างกันระหว่างขอบ (edge) และเนื้อ (face) ใช้ color tuple โดย index ที่ 4 (alpha) กำหนดความทึบ
ขั้นตอน
- ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
- สร้าง figure และ subplots ด้วย subplots()
- กำหนดค่า opacity สำหรับ edge และ face แตกต่างกัน
- เพิ่มสี่เหลี่ยมผืนผ้า (rectangle patch) ด้วย add_patch()
- แสดงรูปด้วย show()
ตัวอย่าง
from matplotlib import pyplot as plt, patches
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
figure, ax = plt.subplots()
edge_color_opacity = 1 # 0 < val < 1
face_color_opacity = 0.75 # 0 < val < 1
ax.add_patch(patches.Rectangle((.25, .25), .50, .50,
edgecolor=(1, 0, 0, edge_color_opacity),
facecolor=(0, 1, 0, face_color_opacity),
linewidth=2))
plt.show()ผลลัพธ์
