My goal is to display several polygons at once (this is data I'm getting from Google Vision API).
I have a list of coordinates in this format:
lst_coord = [['(742,335),(840,334),(840,351),(742,352)'], ['(304,1416),(502,1415),(502,1448),(304,1449)']
Knowing that those are strings:
(742,548),(814,549),(814,563),(742,562)
<class 'str'>
import matplotlib.pyplot as plt
def plotpoly(coord,x,y):
coord.append(coord[0])
x, y = zip(*coord)
plt.plot(x,y)
for coord in lst_coord:
plotpoly(coord,x,y)
plt.show()
I'm getting this error:
AttributeError: 'str' object has no attribute 'append'
I've tried quite a few different things. But I can't get it to work...
Bonus: My final goal is to display those polygons on a picture and I'm also struggling with this...
You can use Polygon
patches, they will close automatically:
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
lst_coord = [['(742,335),(840,334),(840,351),(742,352)'], ['(304,1416),(502,1415),(502,1448),(304,1449)']]
patches = []
for coord in lst_coord:
patches.append(Polygon(eval(coord[0])))
fig, ax = plt.subplots()
ax.add_collection(PatchCollection(patches, fc='none', ec='red'))
ax.set_xlim(0,1000)
ax.set_ylim(0,1500)
plt.show()