Matplotlib 简明教程
Matplotlib - Paths
路径是引导从一处到另一处行进的路线(轨迹)。它可以是真实的,例如踪迹或道路,也可以是抽象的,例如实现目标的一系列步骤。路径提供方向,并帮助我们穿行各种空间。它们标志着到达目的地的一种方式。
A path is a route (track) that guides movement from one place to another. It can be physical, like a trail or road, or abstract, like a series of steps to achieve a goal. Paths provide direction and help us to navigate through spaces. They signify a way to reach a destination.
Paths in Matplotlib
在 Matplotlib 中,路径是用于在图表上绘制形状和线条的基本对象。路径包含一系列连接的点(称为顶点或节点),以及有关如何连接这些点以形成线、曲线或多边形等形状的说明。
In Matplotlib, paths are fundamental objects used to draw shapes and lines on plots. A path consists of a series of connected points, known as vertices or nodes, along with instructions on how to connect these points to form shapes like lines, curves, or polygons.
您可以使用 “Path” 类在 Matplotlib 中创建路径。这些路径可用于创建各种类型的图表,例如线、曲线、多边形,甚至自定义形状。路径提供了一种定义图表中对象外观的方法,允许您控制其大小、位置和样式。
You can create a path in Matplotlib using the "Path" class. These paths can be used to create various types of plots such as lines, curves, polygons, and even custom shapes. Paths provide a way to define the appearance of objects in plots, allowing you to control their size, position, and style.
我们先从绘制一条基本的直线路径开始。
Let’s start by drawing a basic straight line path.
Straight Line Path
在 Matplotlib 中,直线路径是指由一条直线连接的两个点的基本几何元素。此路径通过指定起始点(通常称为“原点”)和线段终点的坐标创建。Matplotlib 然后在这两点之间绘制一条直线,形成路径。
In Matplotlib, a straight line path refers to a basic geometric element consisting of two points connected by a straight line. This path is created by specifying the coordinates of the starting point (often called the "origin") and the ending point of the line. Matplotlib then draws a straight line between these two points, forming the path.
Example
在以下示例中,我们使用 Matplotlib 中的 Path 类创建直线路径。我们定义两个顶点,代表线的起始点和终点。此外,我们指定路径代码,以指示路径移动到第一个顶点,然后创建到第二个顶点的线。通过使用这些顶点和代码构建路径,我们实现了直线表示法 −
In the following example, we are creating a straight line path using the Path class in Matplotlib. We define two vertices representing the starting and ending points of the line. Additionally, we specify the path codes to indicate that the path moves to the first vertex and then creates a line to the second vertex. By constructing the path using these vertices and codes, we achieve the representation of a straight line −
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# Defining the vertices for the straight line path
verts = [
# Starting point
(0, 0),
# Ending point
(1, 1)
]
# Defining the codes for the straight line path
codes = [Path.MOVETO, Path.LINETO]
# Creating the straight line path
path = Path(verts, codes)
# Plotting the path
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)
ax.set_title('Straight Line Path')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.grid(True)
plt.show()
以下是上面代码的输出: -
Following is the output of the above code −
Circle Path
在 Matplotlib 中,圆路径表示绘图上绘制的圆形。它通过指定圆的中心点及其半径来创建的。Matplotlib 然后在圆周周围生成一系列点,将它们连接起来形成闭合路径。
In Matplotlib, a circle path represents a circular shape drawn on a plot. It is created by specifying the center point of the circle and its radius. Matplotlib then generates a series of points around the circumference of the circle, connecting them to form a closed path.
Example
这里我们利用 Matplotlib 的 Path 类创建了一条圆形路径。我们通过定义均匀分布在单位圆周上的角度来生成一系列圆周上的顶点。利用三角函数,我们计算出这些顶点的坐标。随后,我们定义路径代码,使用直线将这些顶点连接起来 −
In here, we are creating a circular path using the Path class in Matplotlib. We generate a sequence of vertices along the circumference of a circle by defining angles uniformly spaced around the unit circle. Using trigonometric functions, we compute the coordinates of these vertices. Subsequently, we define the path codes to connect these vertices with lines −
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import numpy as np
# Defining the angles for the circle path
theta = np.linspace(0, 2*np.pi, 100)
# Defining the vertices for the circle path
verts = np.column_stack([np.cos(theta), np.sin(theta)])
# Definng the codes for the circle path
codes = [Path.MOVETO] + [Path.LINETO] * (len(verts) - 1)
# Creating the circle path
path = Path(verts, codes)
# Plotting the path
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)
ax.set_title('Circle Path')
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.grid(True)
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Bezier Curve Path
在 Matplotlib 中,贝塞尔曲线路径是一条平滑的曲线,利用控制点创建其形状。
In Matplotlib, a Bezier curve path is a smooth, curved line created using control points to define its shape.
为了构建贝塞尔曲线,您需要指定一系列控制点来指导曲线的路径。这些控制点决定了曲线的方向和形状,使您能够创建平滑、流畅的形状。随后 Matplotlib 通过连接这些控制点的点来生成曲线。
To construct a Bezier curve, you specify a series of control points that guide the curve’s path. These control points determine the curve’s direction and shape, allowing you to create smooth, flowing shapes. Matplotlib then generates the curve by connecting the dots these control points.
Example
现在,我们利用 Matplotlib 中的 Path 类创建一条贝塞尔曲线路径。我们指定了定义贝塞尔曲线形状的控制点。此外,我们定义了路径代码来指示连续控制点之间的曲线段类型。通过使用这些控制点和代码构建路径,我们创建了一条平滑的贝塞尔曲线 −
Now, we are creating a Bezier curve path in Matplotlib using the Path class. We specify the control points that define the shape of the Bezier curve. Additionally, we define the path codes to indicate the type of curve segments between consecutive control points. By constructing the path with these control points and codes, we create a smooth Bezier curve −
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# Defining the control points for the Bezier curve
verts = [
(0.1, 0.1),
(0.2, 0.8),
(0.8, 0.8),
(0.9, 0.1),
]
# Defining the codes for the Bezier curve path
codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4]
# Creating the Bezier curve path
path = Path(verts, codes)
# Plotting the path
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)
ax.set_title('Bezier Curve Path')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.grid(True)
plt.show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Polygon Path
在 Matplotlib 中,多边形路径表示一个具有多个直边的闭合形状,类似于几何多边形。此形状通过按照特定顺序指定其顶点(或拐点)的坐标来创建。随后 Matplotlib 按顺序连接这些点以形成多边形的边,并通过将最后一个点连接到第一个点来闭合路径。
In Matplotlib, a polygon path represents a closed shape with multiple straight sides, similar to a geometric polygon. This shape is created by specifying the coordinates of its vertices, or corner points, in a specific order. Matplotlib then connects these points sequentially to form the edges of the polygon and closes the path by connecting the last point to the first.
多边形路径是用于表示各种形状(例如三角形、矩形、五边形或任何其他多边形)的多功能元素。它们可以填充颜色以表示区域,或用作轮廓以突出显示绘图中的特定区域。
Polygon paths are versatile elements used to represent various shapes, such as triangles, rectangles, pentagons, or any other multi-sided figures. They can be filled with color to represent areas or used as outlines to highlight specific regions in plots.
Example
在此示例中,我们利用 Matplotlib 中的 Path 类创建了一条多边形路径。我们定义了多边形的顶点,表示多边形形状的拐点。此外,我们指定了路径代码以连接这些顶点并闭合多边形。通过使用这些顶点和代码构建路径,我们形成一个闭合的多边形形状 −
In this example, we create a polygonal path using the Path class in Matplotlib. We define the vertices of the polygon, representing the corners of the polygonal shape. Additionally, we specify the path codes to connect these vertices and close the polygon. By constructing the path with the vertices and codes, we form a closed polygonal shape −
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# Defining the vertices for the polygon
verts = [
(0.1, 0.1),
(0.5, 0.9),
(0.9, 0.1),
# Closing the polygon
(0.1, 0.1)
]
# Defining the codes for the polygon path
codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
# Creating the polygon path
path = Path(verts, codes)
# Plotting the path
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)
ax.set_title('Polygon Path')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.grid(True)
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −