Python中的 Matplotlib.axes.Axes.add_patch()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.add_patch()函数
matplotlib 库的 axes 模块中的Axes.add_patch()函数用于向轴的补丁添加补丁;返回补丁。
Syntax: Axes.add_patch(self, p)
Parameters: This method accepts the following parameters.
- line: This parameter is the Patch to the axes’ patches.
Return value: This method returns the Patch .
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.add_patch()函数:
示例 1:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
y, x = np.mgrid[:5, 1:6]
poly_coords = [
(0.25, 2.75), (3.25, 2.75),
(2.25, 0.75), (0.25, 0.75)
]
fig, ax = plt.subplots()
cells = ax.plot(x, y, x + y, color ='green')
ax.add_patch(
plt.Polygon(poly_coords, color ='forestgreen',
alpha = 0.5)
)
ax.margins(x = 0.1, y = 0.05)
ax.set_aspect('equal')
fig.suptitle('matplotlib.axes.Axes.add_patch() \
function Example\n\n', fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
Path = mpath.Path
fig, ax = plt.subplots()
pp1 = mpatches.PathPatch(
Path([(0, 0), (1, 0), (1, 1), (0, 0)],
[Path.MOVETO, Path.CURVE3,
Path.CURVE3, Path.CLOSEPOLY]),
fc ="none", transform = ax.transData)
ax.add_patch(pp1)
ax.plot([0.75], [0.25], "go-")
fig.suptitle('matplotlib.axes.Axes.add_patch() \
function Example\n\n', fontweight ="bold")
plt.show()
输出: