Matplotlib 简明教程

Matplotlib - Compound Path

在图形和设计中,“路径”是由线或曲线连接的一系列点。它就像从一个点到另一个点画一条线。“复合”是指由多个部分或元素组成。

想象一下,您有一些基本形状,如圆形、矩形和线。单独来看,这些形状很简单,它们所能表示的内容有限。然而,通过将它们以不同的排列和方向组合在一起,您可以创建更复杂且有趣的形状。这种将简单形状组合成更复杂形状的过程就是使用复合路径。

compound path1

Compound Path in Matplotlib

Matplotlib 中的复合路径是指多个简单路径或形状的组合,这些路径或形状组合在一起形成一个更复杂的形状。这些简单路径可以是线、曲线、多边形或其他基本形状。

在 Matplotlib 中,您可以使用“Path”类创建复合路径,该类允许您通过指定描述路径段的顶点和代码来定义自定义路径。然后,您可以使用这些自定义路径在您的图上绘制复杂的形状或图案。

Compound Path: Intersection of Circle and Rectangle

Matplotlib 中圆形和矩形相交的复合路径是指创建一个代表圆形和矩形重叠或相交区域的单个形状。您没有将圆形和矩形视为单独的实体,而是将它们组合在一起形成一个新的、更复杂的形状,该形状捕捉到它们相交的公共区域。

在 Matplotlib 中,您可以通过定义描述每个形状的路径段的顶点和代码来创建一个圆形和矩形的相交复合路径。然后,您将这些路径组合在一起形成表示相交的复合路径。创建后,您可以使用此复合路径在您的图上绘制相交区域。

Example

在下面示例中,我们通过将圆和矩形的顶点和代码组合到一个 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()

以下是上面代码的输出: -

compound path2

Compound Path of Union of Two Rectangles

Matplotlib 中表示两个矩形并集的复合路径,是指通过合并由两个分开的矩形覆盖的区域来创建一个单一形状。这并不将矩形作为单独形状,而是将它们合并在一起形成一个新的更为复杂的形状。

在 Matplotlib 中,你可以通过定义描述每个矩形路径段的顶点和代码来创建一个两个矩形并集的复合路径。然后,将这些路径组合在一起以形成表示矩形并集的复合路径。创建后,可以使用该复合路径在绘图中绘制合并后的形状。

Example

在这里,我们通过将圆和矩形的顶点和代码组合到一个 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()

执行上述代码,我们将得到以下输出 −

compound path3

Compound Path: Difference of Two Circles

Matplotlib 中表示两个圆差集的复合路径,是指在从一个圆中减去另一个圆时,创建一个表示剩余区域的单一形状。

在 Matplotlib 中,你可以通过定义描述每个圆路径段的顶点和代码来创建一个两个圆差集的复合路径。然后,将这些路径组合在一起并将较小圆的路径从较大圆的路径中减去以形成表示差集的复合路径。创建后,可以使用该复合路径在绘图中绘制剩余的环形区域。

Example

现在,我们通过将两个圆的顶点和代码组合到一个 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()

执行上面的代码后,我们得到以下输出: -

compound path4

Compound Path: Exclusive XOR of Two Polygons

在 Matplotlib 中表示两个多边形的异或(排他或)运算的复合路径表示两个多边形的组合,其中仅保留不重叠的区域。换句话说,它显示了仅属于一个多边形但并不属于两个多边形的区域。

在 Matplotlib 中,你可以通过定义描述每个多边形路径段的顶点和代码来创建两个多边形的异或运算的复合路径。然后,将这些路径组合在一起并应用异或运算以确定仅属于一个或另一个多边形的区域。生成的复合路径表示这些不重叠的区域,你可以使用它们在绘图中绘制。

Example

在此示例中,我们通过将两个多边形的顶点和代码组合到一个 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()

执行上述代码,我们将得到以下输出 −

compound path5