I want to use Shapely for my computational geometry project. I need to be able to visualize and display polygons, lines, and other geometric objects for this. I’ve tried to use Matplotlib for this but I am having trouble with it.
JavaScript
x
11
11
1
from shapely.geometry import Polygon
2
import matplotlib.pyplot as plt
3
4
polygon1 = Polygon([(0,5),
5
(1,1),
6
(3,0),
7
])
8
9
plt.plot(polygon1)
10
plt.show()
11
I would like to be able to display this polygon in a plot. How would I change my code to do this?
Advertisement
Answer
Use:
JavaScript
1
5
1
import matplotlib.pyplot as plt
2
3
x,y = polygon1.exterior.xy
4
plt.plot(x,y)
5
Or, more succinctly:
JavaScript
1
2
1
plt.plot(*polygon1.exterior.xy)
2