Home > OS >  Python: Image Background on a plot
Python: Image Background on a plot

Time:03-20

I have this plot in which I can adapt the curve as I want. My problem is I need to draw on an image. I don´t know how to put both together. 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()

CodePudding user response:

Try call ax.imshow before draw the polygon? Like this:

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()

BTW, your xlim and ylim is also not proper. Your image is in the range of y=0~700, but your polygon is y=1000~1300. You at least need to ax.set_ylim([0,1400]) for your image and polygon shown together.

  • Related