matplotlib - Python & Matplot: How can I draw a simple shape by points? -


i want draw simple shape points, this:

rectangle = [(0,0),(0,1),(1,1),(1,0)] hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)] l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)] concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]  points in [rectangle, hexagon, l_shape, concave]:     # 1. can rid of zip? plot directly points      # 2. how can make shape complete?     xs, ys = zip(*points)     plt.plot(xs, ys, 'o')     plt.plot(xs, ys, '-')      automin, automax = plt.xlim()     plt.xlim(automin-0.5, automax+0.5)     automin, automax = plt.ylim()     plt.ylim(automin-0.5, automax+0.5)     # can display shapes 2 in 1 line?     plt.show() 

my question enter image description here

  1. how can rid of *zip? mean, directyly draw points, rather 2 array.
  2. how make these shapes complete? since i'm looping through points, first , last cannot connect together, how can it?
  3. can draw shape without giving specific points order?(something convex hull?)

the code below doesn't use temporary variables xs , ys, direct tuple unpacking. add first point points list make shapes complete.

rectangle = [(0,0),(0,1),(1,1),(1,0)] hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)] l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)] concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]  points in [rectangle, hexagon, l_shape, concave]:     plt.plot(*zip(*(points+points[:1])), marker='o')      automin, automax = plt.xlim()     plt.xlim(automin-0.5, automax+0.5)     automin, automax = plt.ylim()     plt.ylim(automin-0.5, automax+0.5)      plt.show() 

provide answer alternative leekaiinthesky's post


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -