Matplotlib 简明教程

Matplotlib - Compound Path

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

In graphics and design, a "path" is a sequence of points connected by lines or curves. It is like drawing a line from one point to another. "Compound" means made up of multiple parts or elements.

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

Imagine you have several basic shapes like circles, rectangles, and lines. Individually, these shapes are simple and limited in what they can represent. However, by combining them together in different arrangements and orientations, you can create more complex and interesting shapes. This process of combining simple shapes to create more complex ones is what is referred to as using compound paths.

compound path1

Compound Path in Matplotlib

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

A compound path in Matplotlib refers to a combination of multiple simple paths or shapes that are grouped together to form a more complex shape. These simple paths could be lines, curves, polygons, or other basic shapes.

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

In Matplotlib, you can create compound paths using the "Path" class, which allows you to define custom paths by specifying the vertices and codes that describe the path segments. You can then use these custom paths to draw complex shapes or patterns on your plots.

Compound Path: Intersection of Circle and Rectangle

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

A compound path of the intersection of a circle and a rectangle in Matplotlib refers to creating a single shape that represents the area where the circle and the rectangle overlap or intersect. Instead of treating the circle and the rectangle as separate entities, you combine them together to form a new, more complex shape that captures the common region where they intersect.

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

In Matplotlib, you can create a compound path of the intersection of a circle and a rectangle by defining the vertices and codes that describe the path segments for each shape. Then, you combine these paths together to form the compound path representing the intersection. Once created, you can use this compound path to draw the intersecting area on your plot.

Example

在下面示例中,我们通过将圆和矩形的顶点和代码组合到一个 Path 对象中来创建一个表示圆和矩形的交集的复合路径:

In the following example, we are creating a compound path representing the intersection of a circle and a rectangle by combining their vertices and codes into a single Path object −

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()

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

Following is the output of the above code −

compound path2

Compound Path of Union of Two Rectangles

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

A compound path of the union of two rectangles in Matplotlib refers to creating a single shape by combining the areas covered by two separate rectangles. Instead of treating the rectangles as individual shapes, you merge them together to form a new, more complex shape.

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

In Matplotlib, you can create a compound path of the union of two rectangles by defining the vertices and codes that describe the path segments for each rectangle. Then, you combine these paths together to form the compound path representing the union of the rectangles. Once created, you can use this compound path to draw the combined shape on your plot.

Example

在这里,我们通过将圆和矩形的顶点和代码组合到一个 Path 对象中来创建一个表示这两个矩形并集的复合路径:

In here, we are creating a compound path representing the union of two rectangles by combining their vertices and codes into a single Path object −

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()

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

On executing the above code we will get the following output −

compound path3

Compound Path: Difference of Two Circles

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

A compound path of the difference of two circles in Matplotlib refers to creating a single shape that represents the area remaining when one circle is subtracted from another.

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

In Matplotlib, you can create a compound path of the difference of two circles by defining the vertices and codes that describe the path segments for each circle. Then, you combine these paths together and subtract the path of the smaller circle from the path of the larger circle to form the compound path representing the difference. Once created, you can use this compound path to draw the remaining ring-shaped area on your plot.

Example

现在,我们通过将两个圆的顶点和代码组合到一个 Path 对象中来创建一个表示两个圆差集的复合路径:

Now, we are creating a compound path representing the difference between two circles by combining their vertices and codes into a single Path object −

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()

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

After executing the above code, we get the following output −

compound path4

Compound Path: Exclusive XOR of Two Polygons

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

A compound path of the exclusive XOR (exclusive OR) of two polygons in Matplotlib represents a combination of the two polygons where only the areas that do not overlap are retained. In other words, it shows the regions that belong exclusively to one of the polygons, but not to both.

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

In Matplotlib, you can create a compound path of the exclusive XOR of two polygons by defining the vertices and codes that describe the path segments for each polygon. Then, you combine these paths together and apply the exclusive XOR operation to determine the regions that belong exclusively to one polygon or the other. The resulting compound path represents these non-overlapping areas, which you can use to draw on your plot.

Example

在此示例中,我们通过将两个多边形的顶点和代码组合到一个 Path 对象中来创建一个表示两个多边形异或运算的复合路径:

In this example, we are creating a compound path representing the exclusive XOR operation on two polygons by combining their vertices and codes into a single Path object −

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()

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

On executing the above code we will get the following output −

compound path5