通过以正确的格式获取Google Vision API数据,使用matplotlib一次显示多个多边形

通过以正确的格式获取Google Vision API数据,使用matplotlib一次显示多个多边形,matplotlib,polygon,google-cloud-vision,google-vision,Matplotlib,Polygon,Google Cloud Vision,Google Vision,我的目标是一次显示几个多边形(这是我从Google Vision API获得的数据) 我有一个坐标列表,格式如下: lst_coord = [['(742,335),(840,334),(840,351),(742,352)'], ['(304,1416),(502,1415),(502,1448),(304,1449)'] 知道这些是字符串: (742,548),(814,549),(814,563),(742,562) <class 'str'> 我得到了这个错误: Attr

我的目标是一次显示几个多边形(这是我从Google Vision API获得的数据)

我有一个坐标列表,格式如下:

lst_coord = [['(742,335),(840,334),(840,351),(742,352)'], ['(304,1416),(502,1415),(502,1448),(304,1449)']
知道这些是字符串:

(742,548),(814,549),(814,563),(742,562)
<class 'str'>
我得到了这个错误:

AttributeError: 'str' object has no attribute 'append'
我试过很多不同的方法。但我不能让它工作

奖励:我的最终目标是在图片上显示这些多边形,我也在努力解决这个问题…

您可以使用补丁,它们将自动关闭:

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

太好了,谢谢!如果我想用不同的颜色绘制这两个多边形中的每一个呢?您可以在for循环中为每个多边形设置颜色,然后在
PatchCollection
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()