Matplotlib - 複合路徑



在圖形和設計中,“路徑”是指由線或曲線連線的一系列點。它就像從一個點到另一個點畫一條線。“複合”表示由多個部分或元素組成。

想象一下,您有幾個基本形狀,例如圓形、矩形和線條。單獨來看,這些形狀很簡單,並且在它們可以表示的內容方面受到限制。但是,透過將它們以不同的排列和方向組合在一起,您可以建立更復雜和有趣的形狀。這個將簡單形狀組合以建立更復雜形狀的過程就是所謂的使用複合路徑。

Compound Path

Matplotlib 中的複合路徑

Matplotlib 中的複合路徑是指多個簡單路徑或形狀的組合,這些路徑或形狀組合在一起形成更復雜的形狀。這些簡單路徑可以是線、曲線、多邊形或其他基本形狀。

在 Matplotlib 中,您可以使用“Path”類建立複合路徑,該類允許您透過指定描述路徑段的頂點和程式碼來定義自定義路徑。然後,您可以使用這些自定義路徑在繪圖上繪製複雜的形狀或圖案。

複合路徑:圓形和矩形的交集

Matplotlib 中圓形和矩形交集的複合路徑是指建立一個表示圓形和矩形重疊或相交區域的單個形狀。您不是將圓形和矩形視為單獨的實體,而是將它們組合在一起以形成一個新的、更復雜的形狀,該形狀捕獲它們相交的公共區域。

在 Matplotlib 中,您可以透過定義描述每個形狀的路徑段的頂點和程式碼來建立圓形和矩形交集的複合路徑。然後,您將這些路徑組合在一起以形成表示交集的複合路徑。建立後,您可以使用此複合路徑在繪圖上繪製相交區域。

示例

在以下示例中,我們透過將它們的頂點和程式碼組合到單個 Path 物件中來建立一個表示圓形和矩形交集的複合路徑 -

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Defining the paths for the shapes
circle = Path.unit_circle()
rect = Path([(0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5), (0.5, 0.5)], closed=True)

# Combining the paths into a compound path (Intersection)
compound_vertices = circle.vertices.tolist() + rect.vertices.tolist()
compound_codes = circle.codes.tolist() + rect.codes.tolist()
compound_path = Path(compound_vertices, compound_codes)

# Plotting the compound path
fig, ax = plt.subplots()
patch = PathPatch(compound_path, facecolor='blue', edgecolor='black')
ax.add_patch(patch)

# Setting plot limits and aspect ratio
ax.set_xlim(0, 2)
ax.set_ylim(0, 2)
ax.set_aspect('equal')

# Showing the plot
plt.title('Intersection of Circle and Rectangle')
plt.show()

輸出

以下是上述程式碼的輸出 -

Intersection of Circle and Rectangle

兩個矩形並集的複合路徑

Matplotlib 中兩個矩形並集的複合路徑是指透過組合兩個獨立矩形覆蓋的區域來建立一個單個形狀。您不是將矩形視為單個形狀,而是將它們合併在一起以形成一個新的、更復雜的形狀。

在 Matplotlib 中,您可以透過定義描述每個矩形的路徑段的頂點和程式碼來建立兩個矩形並集的複合路徑。然後,您將這些路徑組合在一起以形成表示矩形並集的複合路徑。建立後,您可以使用此複合路徑在繪圖上繪製組合形狀。

示例

在這裡,我們透過將它們的頂點和程式碼組合到單個 Path 物件中來建立一個表示兩個矩形並集的複合路徑 -

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Defining the paths for the shapes
# Rectangle 1
path1 = Path([(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)], closed=True)
# Rectangle 2
path2 = Path([(1.5, 0), (1.5, 1), (2.5, 1), (2.5, 0), (1.5, 0)], closed=True)  

# Combining the paths into a compound path (Union)
compound_vertices = path1.vertices.tolist() + path2.vertices.tolist()
compound_codes = path1.codes.tolist() + path2.codes.tolist()
compound_path = Path(compound_vertices, compound_codes)

# Plotting the compound path
fig, ax = plt.subplots()
patch = PathPatch(compound_path, facecolor='blue', edgecolor='black')
ax.add_patch(patch)

# Setting plot limits and aspect ratio
ax.set_xlim(0, 3)
ax.set_ylim(0, 2.5)
ax.set_aspect('equal')

# Displaying the plot
plt.title('Union of Two Rectangles')
plt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出 -

Union of Two Rectangles

複合路徑:兩個圓形的差集

Matplotlib 中兩個圓形差集的複合路徑是指建立一個表示從另一個圓形中減去一個圓形後剩餘區域的單個形狀。

在 Matplotlib 中,您可以透過定義描述每個圓形的路徑段的頂點和程式碼來建立兩個圓形差集的複合路徑。然後,您將這些路徑組合在一起,並將較小圓形的路徑從較大圓形的路徑中減去,以形成表示差集的複合路徑。建立後,您可以使用此複合路徑在繪圖上繪製剩餘的環形區域。

示例

現在,我們透過將它們的頂點和程式碼組合到單個 Path 物件中來建立一個表示兩個圓形之間差異的複合路徑 -

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import numpy as np

# Defining the paths for the shapes
circle1 = Path.unit_circle()
circle2 = Path.unit_circle().transformed(plt.matplotlib.transforms.Affine2D().scale(0.5))

# Combining the paths into a compound path (Difference)
compound_vertices = np.concatenate([circle1.vertices, circle2.vertices])
compound_codes = np.concatenate([circle1.codes, circle2.codes])
compound_path = Path(compound_vertices, compound_codes)

# Plotting the compound path
fig, ax = plt.subplots()
patch = PathPatch(compound_path, facecolor='blue', edgecolor='black')
ax.add_patch(patch)

# Setting plot limits and aspect ratio
ax.set_xlim(-1, 2)
ax.set_ylim(-1, 2)
ax.set_aspect('equal')

# Displaying the plot
plt.title('Difference of Two Circles')
plt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出 -

Difference of Two Circles

複合路徑:兩個多邊形的異或

Matplotlib 中兩個多邊形的異或(異或)的複合路徑表示兩個多邊形的組合,其中僅保留不重疊的區域。換句話說,它顯示了專屬於其中一個多邊形但不屬於兩個多邊形的區域。

在 Matplotlib 中,您可以透過定義描述每個多邊形的路徑段的頂點和程式碼來建立兩個多邊形異或的複合路徑。然後,您將這些路徑組合在一起並應用異或運算以確定專屬於一個多邊形或另一個多邊形的區域。生成的複合路徑表示這些不重疊的區域,您可以使用它們在繪圖上進行繪製。

示例

在此示例中,我們透過將它們的頂點和程式碼組合到單個 Path 物件中來建立一個表示對兩個多邊形執行異或運算的複合路徑 -

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Defining the paths for the shapes
poly1 = Path([(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)], closed=True)  # Rectangle
poly2 = Path([(1.5, 0), (1.5, 1), (2.5, 1), (2.5, 0), (1.5, 0)], closed=True)  # Triangle

# Combining the paths into a compound path (Exclusive XOR)
compound_vertices = poly1.vertices.tolist() + poly2.vertices.tolist()
compound_codes = poly1.codes.tolist() + poly2.codes.tolist()
compound_path = Path(compound_vertices, compound_codes)

# Plotting the compound path
fig, ax = plt.subplots()
patch = PathPatch(compound_path, facecolor='blue', edgecolor='black')
ax.add_patch(patch)

# Setting plot limits and aspect ratio
ax.set_xlim(0, 3)
ax.set_ylim(0, 2.5)
ax.set_aspect('equal')

# Displaying the plot
plt.title('Exclusive XOR of Two Polygons')
plt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出 -

Exclusive XOR of Two PolygonsBox
廣告