Как построить соседние полигоны в python с учетом координат X и Y? - PullRequest
0 голосов
/ 06 апреля 2020

Я новичок в построении изображений в 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()

1 Ответ

2 голосов
/ 06 апреля 2020

Возможно, вы захотите нарисовать полигоны один за другим. plt.plot соединит все точки в списке со следующей, не оставляя пробелов.

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': {}}]
plt.figure()
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']
        plt.plot(x_cor+x_cor[:1], y_cor+y_cor[:1])
plt.show()

example plot

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...