如何使用 Matplotlib 在 Python 中填充多邊形中的區域?


若要使用 Matplotlib 在 Python 中填充多邊形中的區域,我們可以採取以下步驟 −

步驟

  • 設定圖形大小,調整子圖之間的邊距和子圖周圍的邊距。

  • 建立圖形和一組子圖。

  • 獲取多邊形的例項。

  • 獲取具有可迭代多邊形的通用補丁集合。

  • 向軸的集合新增一個“集合”;返回集合。

  • 要顯示圖形,請使用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()

輸出

它會產生以下輸出 −

更新於: 02-Feb-2022

2K+ 瀏覽

開啟你的 事業

參加課程並獲得認證

開始
廣告
© . All rights reserved.