Ste*_*ein 7 python image matplotlib
我正在使用Matplotlib画一幅画:
plt.imshow(bild)
plt.show()
Run Code Online (Sandbox Code Playgroud)
如何使用图像的坐标为此添加标记(例如,红点/箭头)?
您还可以使用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()
Run Code Online (Sandbox Code Playgroud)
您可以使用模块matplotlib.patches,如下所示。请注意,为了把补丁在X个行ÿ个图像的列,你需要扭转的坐标,即顺序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)
Run Code Online (Sandbox Code Playgroud)
您可以使用该函数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()
Run Code Online (Sandbox Code Playgroud)