Home > Blockchain >  How to draw polygons in python (giving cordenates) and number its edges?
How to draw polygons in python (giving cordenates) and number its edges?

Time:02-03

I have a list of agroforestry plots (polygons) in python. Every polygon es represented as latitude/longitude.

polygon = [
    (12.132481, -86.239511),
    (12.132093,-86.239344 ),
    (12.132287, -86.23876),
    (12.132481, -86.239511)
]

I need a way to draw those polygons as an image and place numbers on its edges. A polygon need to also be represented with a letter. This is an example of what I need

Polygons I need to draw

How can I do it in python?

CodePudding user response:

There are several libraries in Python that allow you to draw polyggon shapes and annotate them. Here's an example using the Matplotlib library:

import matplotlib.pyplot as plt

polygon = [
    (12.132481, -86.239511),
    (12.132093,-86.239344 ),
    (12.132287, -86.23876),
    (12.132481, -86.239511)
]

x = [p[0] for p in polygon]
y = [p[1] for p in polygon]

fig, ax = plt.subplots()
ax.plot(x, y)

for i, p in enumerate(polygon):
    ax.annotate(f"{i 1}", (p[0], p[1]))

plt.show()

This code will display the polygon as a series of connected line segments and annotate each vertex with its edge number. To represent the polygon with a letter, simply replace the f"{i 1}" with the desired letter in the ax.annotate function.

CodePudding user response:

Borrowing from the previous answer, but adding the label not just at the edges, but also inside the polygon, you can use shapely's polygon plot with labels

  • Related