Python:图上的图像背景

Python: Image Background on a plot

我有这个图,我可以根据需要调整曲线。我的问题是我需要在图像上绘图。我不知道如何把两者放在一起。 1

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

#theta = np.arange(0, 2*np.pi, 0.1)
#r = 1.5

#xs = r*np.cos(theta)
#ys = r*np.sin(theta)
xs = (921, 951, 993, 1035, 1065, 1045, 993, 945)
ys = (1181, 1230, 1243, 1230, 1181, 1130, 1130, 1130)

poly = Polygon(list(zip(xs, ys)), animated=True)


fig, ax = plt.subplots()
ax.add_patch(poly)
p = PolygonInteractor(ax, poly, visible=False)

ax.set_title('Click and drag a point to move it')

ax.set_xlim((800, 1300))
ax.set_ylim((1000, 1300))

plt.show()

在绘制多边形之前尝试调用 ax.imshow?像这样:

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

from scipy import misc


xs = (21, 51, 93, 135, 100, 90, 21, 10)
ys = (111, 130, 143, 230, 501, 530, 530, 513)

poly = Polygon(list(zip(xs, ys)), color='r')

fig, ax = plt.subplots()

ax.imshow(misc.face(), origin='lower')

ax.add_patch(poly)

# ax.set_xlim([0,2000])
# ax.set_ylim([0,2000])

fig.show()

顺便说一句,你的 xlim 和 ylim 也不合适。你的图像在y=0~700的范围内,但你的多边形是y=1000~1300。您至少需要 ax.set_ylim([0,1400]) 将图像和多边形一起显示。