เพื่อระบายสีบริเวณทับซ้อนของวงกลมหรือแพทช์ใน Matplotlib ให้ทำตามขั้นตอน:
- กำหนดขนาดรูปและปรับระยะห่างระหว่างและรอบซับพล็อต
- สร้างจุด a และ b
- หาพื้นที่ซ้าย ขวา และกลางจากจุดทั้งสอง a และ b
- ดึงแกนปัจจุบันด้วย gca()
- เพิ่มแพทช์พร้อมสีและส่วนต่างๆ
- ตั้งค่ามقياسแกน X และ Y
- ตั้งค่าอัตราส่วนภาพเท่ากัน
- ปิดการแสดงแกน
- ใช้ show() เพื่อแสดงรูป
ตัวอย่าง
import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
a = sg.Point(-.5, 0).buffer(1.)
b = sg.Point(0.5, 0).buffer(1.)
left = a.difference(b)
right = b.difference(a)
middle = a.intersection(b)
ax = plt.gca()
ax.add_patch(descartes.PolygonPatch(left, fc='b', ec='k'))
ax.add_patch(descartes.PolygonPatch(right, fc='r', ec='k'))
ax.add_patch(descartes.PolygonPatch(middle, fc='g', ec='k'))
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_aspect('equal')
ax.axis('off')
plt.show()ผลลัพธ์
