【问题标题】:How to plot adjoining polygons in python given the coordinates of X and Y?给定X和Y的坐标,如何在python中绘制相邻的多边形?
【发布时间】:2020-04-06 17:46:45
【问题描述】:

我不熟悉在 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()

【问题讨论】:

标签: python matplotlib


【解决方案1】:

您可能想一一绘制多边形。 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()

【讨论】:

  • 非常感谢您解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 2021-07-11
  • 1970-01-01
  • 2022-11-30
相关资源
最近更新 更多