如何在Python中的绘图中添加补丁?
Matplotlib是一个出色的Python可视化库,用于数组的 2D 绘图。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。并且 Axes 的实例通过 callbacks 属性支持回调。 matplotlib 库的axes 模块中的Matplotlib.axes.Axes.add_patch() 方法用于向轴的patch 添加一个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.
下面是描述如何在Python中的绘图中添加补丁的各种示例:
示例 1:
Python3
# import modules
import numpy as np
import matplotlib.pyplot as plt
# adjust figure and assign coordinates
y, x = np.mgrid[:5, 1:6]
poly_coords = [(25, 75), (25, 75), (25, 75), (25, 75)]
fig, ax = plt.subplots()
# depict illustration
cells = ax.plot(x, y, x + y)
ax.add_patch(plt.Polygon(poly_coords))
plt.show()
Python3
# import modules
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
Path = mpath.Path
# adjust figure and assign coordinates
fig, ax = plt.subplots()
pp = mpatches.PathPatch(Path([(0, 0), (10, 5), (10, 10), (20, 10)],
[Path.MOVETO, Path.CURVE3,
Path.CURVE3, Path.CLOSEPOLY]),
transform=ax.transData)
# depict illustration
ax.add_patch(pp)
plt.show()
Python3
# import modules
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
# adjust figure and assign coordinates
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
pp1 = plt.Rectangle((0.2, 0.75),
0.4, 0.15)
pp2 = plt.Circle((0.7, 0.2), 0.15)
pp3 = plt.Polygon([[0.15, 0.15],
[0.35, 0.4],
[0.2, 0.6]])
# depict illustrations
ax.add_patch(pp1)
ax.add_patch(pp2)
ax.add_patch(pp3)
Python3
# import module
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import matplotlib.pyplot as plt
import numpy as np
# assign coordinates
coord = [(0, 20), (20, 20), (20, 20),
(20, 20), (10, 10), (10, 10),
(5, 5), (15, 5), (0, 0)]
instn = [Path.MOVETO, Path.LINETO, Path.LINETO,
Path.LINETO, Path.CLOSEPOLY, Path.MOVETO,
Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
# adjust figure
coord = np.array(coord, float)
path = Path(coord, instn)
pathpatch = PathPatch(path)
fig, ax = plt.subplots()
ax.add_patch(pathpatch)
# depict illustration
ax.autoscale_view()
plt.show()
输出:
示例 2:
蟒蛇3
# import modules
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
Path = mpath.Path
# adjust figure and assign coordinates
fig, ax = plt.subplots()
pp = mpatches.PathPatch(Path([(0, 0), (10, 5), (10, 10), (20, 10)],
[Path.MOVETO, Path.CURVE3,
Path.CURVE3, Path.CLOSEPOLY]),
transform=ax.transData)
# depict illustration
ax.add_patch(pp)
plt.show()
输出:
示例 3:
蟒蛇3
# import modules
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
# adjust figure and assign coordinates
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
pp1 = plt.Rectangle((0.2, 0.75),
0.4, 0.15)
pp2 = plt.Circle((0.7, 0.2), 0.15)
pp3 = plt.Polygon([[0.15, 0.15],
[0.35, 0.4],
[0.2, 0.6]])
# depict illustrations
ax.add_patch(pp1)
ax.add_patch(pp2)
ax.add_patch(pp3)
输出:
示例 4:
蟒蛇3
# import module
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import matplotlib.pyplot as plt
import numpy as np
# assign coordinates
coord = [(0, 20), (20, 20), (20, 20),
(20, 20), (10, 10), (10, 10),
(5, 5), (15, 5), (0, 0)]
instn = [Path.MOVETO, Path.LINETO, Path.LINETO,
Path.LINETO, Path.CLOSEPOLY, Path.MOVETO,
Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
# adjust figure
coord = np.array(coord, float)
path = Path(coord, instn)
pathpatch = PathPatch(path)
fig, ax = plt.subplots()
ax.add_patch(pathpatch)
# depict illustration
ax.autoscale_view()
plt.show()
输出: