问题
I'm drawing a picture using Matplotlib:
plt.imshow(bild)
plt.show()
How do I add a Marker to this (eg. red dot / arrow) using the coordinates of the image?
回答1:
You can also use plt.scatter to add a red dot to mark the point. Building on the previous answer's example code:
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()
回答2:
You could use the module matplotlib.patches as shown below. Notice that in order to place a patch at the xth row and yth column of the image you need to reverse the order of the coordinates, i.e. y, x
when instantiating the corresponding patch.
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)
回答3:
You can use the function plt.annotate for this:
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()
来源:https://stackoverflow.com/questions/32551536/draw-marker-in-image