【发布时间】:2015-09-13 15:54:02
【问题描述】:
我正在使用 Matplotlib 绘制图片:
plt.imshow(bild)
plt.show()
如何使用图像的坐标为此添加标记(例如红点/箭头)?
【问题讨论】:
标签: python image matplotlib
我正在使用 Matplotlib 绘制图片:
plt.imshow(bild)
plt.show()
如何使用图像的坐标为此添加标记(例如红点/箭头)?
【问题讨论】:
标签: python image matplotlib
您也可以使用plt.scatter添加一个红点来标记该点。基于上一个答案的示例代码:
import matplotlib.pyplot as plt
import numpy as np
img = np.random.randn(100, 100)
plt.figure()
plt.imshow(img)
plt.annotate('25, 50', xy=(25, 50), xycoords='data',
xytext=(0.5, 0.5), textcoords='figure fraction',
arrowprops=dict(arrowstyle="->"))
plt.scatter(25, 50, s=500, c='red', marker='o')
plt.show()
【讨论】:
您可以使用模块matplotlib.patches,如下所示。请注意,为了在 xth 行和 yth 放置补丁 对应patch 实例化时需要将图片列的坐标顺序颠倒,即y, x。
from skimage import io
import matplotlib.pyplot as plt
from matplotlib.patches import Arrow, Circle
maze = io.imread('https://i.stack.imgur.com/SQCy9.png')
ax, ay = 300, 25
dx, dy = 0, 75
cx, cy = 300, 750
patches = [Arrow(ay, ax, dy, dx, width=100., color='green'),
Circle((cy, cx), radius=25, color='red')]
fig, ax = plt.subplots(1)
ax.imshow(maze)
for p in patches:
ax.add_patch(p)
plt.show(fig)
【讨论】:
您可以为此使用函数plt.annotate:
import matplotlib.pyplot as plt
import numpy as np
img = np.random.randn(100, 100)
plt.imshow(img)
plt.annotate('25, 50', xy=(25, 40), xycoords='data',
xytext=(0.5, 0.5), textcoords='figure fraction',
arrowprops=dict(arrowstyle="->"))
plt.show()
【讨论】: