ในการเติมสีพื้นที่ภายในหลายเหลี่ยมใน Python ด้วย matplotlib สามารถทำตามขั้นตอนดังนี้:
ขั้นตอน
ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
สร้าง figure และชุด subplots
สร้างอินสแตนซ์ของ polygon
สร้าง generic collection of patches ด้วย iterable polygons
เพิ่ม collection ให้กับ axes collections คืนค่า collection
แสดงรูปด้วยเมธอด show()
ตัวอย่าง
import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots(1) polygon = Polygon(np.random.rand(6, 2), closed=True, alpha=1) collection = PatchCollection([polygon]) ax.add_collection(collection) plt.show()
ผลลัพธ์
จะแสดงหลายเหลี่ยมสุ่ม 6 จุด ถูกเติมสี
