I have input values of x, y coordinates in the following format:
[[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]
I want to draw polygons, but I don't know how to draw them!
Thanks
polylines() method is used to draw a polygon on any image. Parameters: image: It is the image on which circle is to be drawn. pts: Array of polygonal curves.
import matplotlib. pyplot as plt coord = [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]] coord. append(coord[0]) #repeat the first point to create a 'closed loop' xs, ys = zip(*coord) #create lists of x and y values plt. figure() plt.
Using matplotlib.pyplot
import matplotlib.pyplot as plt coord = [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]] coord.append(coord[0]) #repeat the first point to create a 'closed loop' xs, ys = zip(*coord) #create lists of x and y values plt.figure() plt.plot(xs,ys) plt.show() # if you need...
Another way to draw a polygon is this:
import PIL.ImageDraw as ImageDraw import PIL.Image as Image image = Image.new("RGB", (640, 480)) draw = ImageDraw.Draw(image) # points = ((1,1), (2,1), (2,2), (1,2), (0.5,1.5)) points = ((100, 100), (200, 100), (200, 200), (100, 200), (50, 150)) draw.polygon((points), fill=200) image.show()
Note that you need to install the pillow library. Also, I scaled up your coordinates by the factor of 100 so that we can see the polygon on the 640 x 480 screen.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With