python - 将补丁添加到在 matplotlib 中打开的 numpy 数组

标签 python numpy matplotlib

我想在从 matplotlib 中的数组打开的图像上绘制方框。我发现绘制方框的一种方法是使用 add_patch ,但我找不到在从数组加载的图像上使用它的方法。

这段代码

arr = np.random.rand(400,400)
fig = plt.imshow(arr)
fig.add_patch(patches.Rectangle((100, 100), 100, 100, fill=False))

产生错误:AttributeError:'AxesImage'对象没有属性'add_patch'

最佳答案

您必须将补丁添加到 matplotlib 轴:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

arr = np.random.rand(400,400)
fig,ax = plt.subplots(1)

ax.imshow(arr)
rect = patches.Rectangle((100, 100), 100, 100, fill=False)
ax.add_patch(rect)
plt.show()

关于python - 将补丁添加到在 matplotlib 中打开的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40580742/

相关文章:

python - 从圆形边框内提取文本

python - bash 脚本中的间歇性语法错误涉及 echo、python 脚本、grep、sed、bc 和 date

python - 骰子组合的标准差

python - 将 numpy 数组列表保存到磁盘上

python - 绘制每条线都有分类列的折线图

python - 由于 "perfect separation error",无法运行逻辑回归

python - 使用 PyQt5 和 Matplotlib 制作动态图

python - 如何为 dlib 训练创建 `testing.xml` 文件?

python - tabulate:为每列定义 floatfmt

python - 从 sklearn 中的高斯混合模型获取 PDF