在给定X和Y坐标的情况下,如何在python中绘制相邻多边形?

在给定X和Y坐标的情况下,如何在python中绘制相邻多边形?,python,matplotlib,Python,Matplotlib,我不熟悉用python绘制图像。谢谢你帮我解决这个问题。我有一个列表,其中包含对象字典,以及相邻多边形的X和Y坐标列表。我能够提取X和Y坐标,但当我使用Matplotlib绘制点时,我没有得到正确的多边形形状 import matplotlib.pyplot as plt shapes = [{'shape_attributes': {'name': 'polygon', 'all_points_x': [35, 28, 27, 31, 40, 51, 62, 72, 74, 71, 65,

我不熟悉用python绘制图像。谢谢你帮我解决这个问题。我有一个列表,其中包含对象字典,以及相邻多边形的X和Y坐标列表。我能够提取X和Y坐标,但当我使用Matplotlib绘制点时,我没有得到正确的多边形形状

import matplotlib.pyplot as plt

shapes = [{'shape_attributes': {'name': 'polygon', 'all_points_x': [35, 28, 27, 31, 40, 51, 62, 72, 74, 71, 65, 57, 41], 'all_points_y': [74, 55, 32, 16, 4, 6, 12, 35, 56, 74, 83, 86, 81]}, 'region_attributes': {}}, None, {'shape_attributes': {'name': 'polygon', 'all_points_x': [6, 16, 24, 44, 69, 77, 81, 82, 80, 76, 69, 62, 51, 26, 9, 7], 'all_points_y': [85, 77, 78, 83, 92, 100, 106, 115, 118, 120, 122, 125, 126, 112, 98, 92]}, 'region_attributes': {}}]

shapesCordinates = []
for shape in shapes:
    if shape is not None:
        x_cor = shape['shape_attributes']['all_points_x']
        y_cor = shape['shape_attributes']['all_points_y']
        for x, y in zip(x_cor, y_cor):
            shapesCordinates.append((x, y))
print(shapesCordinates)
shapesCordinates.append(shapesCordinates[0])
xs, ys = zip(*shapesCordinates)
plt.figure()
plt.plot(xs,ys) 
plt.show()

您可能希望逐个绘制多边形
plt.plot
将列表中的所有点连接到下一个点,而不会留下间隙

导入matplotlib.pyplot作为plt
形状={'shape_attributes':{'name':'polygon','all_points_x':[35,28,27,31,40,51,62,72,74,71,65,57,41],'all_points_y':[74,55,32,16,4,6,12,35,56,74,83,86,81]},{'shape_attributes':{'name':'polygon','all_points_x':[6,16,24,44,69,77,81,35,56,74,83,86,82,51],“所有点:[85,77,78,83,92,100,106,115,118,120,122,125,126,112,98,92],“区域属性:{}]
plt.图()
对于形状中的形状:
如果形状不是无:
x_cor=shape['shape_attributes']['all_points_x']
y_cor=shape['shape_attributes']['all_points\u y']
plt.绘图(x_cor+x_cor[:1],y_cor+y_cor[:1])
plt.show()

您可能希望逐个绘制多边形
plt.plot
将列表中的所有点连接到下一个点,而不会留下间隙

导入matplotlib.pyplot作为plt
形状={'shape_attributes':{'name':'polygon','all_points_x':[35,28,27,31,40,51,62,72,74,71,65,57,41],'all_points_y':[74,55,32,16,4,6,12,35,56,74,83,86,81]},{'shape_attributes':{'name':'polygon','all_points_x':[6,16,24,44,69,77,81,35,56,74,83,86,82,51],“所有点:[85,77,78,83,92,100,106,115,118,120,122,125,126,112,98,92],“区域属性:{}]
plt.图()
对于形状中的形状:
如果形状不是无:
x_cor=shape['shape_attributes']['all_points_x']
y_cor=shape['shape_attributes']['all_points\u y']
plt.绘图(x_cor+x_cor[:1],y_cor+y_cor[:1])
plt.show()

非常感谢你解决了我的问题非常感谢你解决了我的问题