简体   繁体   中英

How to draw the union shape of rectangles in python

I'm using Matplotlib and Python. I want to draw the union of a group of rectangles. The rectangles can be connected or disconnected. I want also to assign a different color to the sides in common with other groups knowing that there is no overlapping regions between groups. Do you have any idea?

Thanks for your help.

I added code for more precision, I have tried to make a collection for each group of rectangles and give them same edge color but how to get only one shape (the perimeter of the group of rectangles)?

import numpy as np
import matplotlib
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt


fig=plt.figure()
ax=fig.add_subplot(111)
patches = []
ListCollections=[]

while Cd1:
  while Cd2:
      patches += Rectangle((x,y), 400, 200)

  p = PatchCollection(patches, cmap=None)
  p.set_edgecolor('red')
  p.set_facecolor(None)
  ListCollections.append(p)
  patches =[]


for l in ListCollections:
   ax.add_collection(p)

plt.show()

Take a look at Shapely. There is an explicit union example http://toblerity.github.com/shapely/manual.html#object.union .

To plot the Shapely geometries you might also want to use https://pypi.python.org/pypi/descartes .

Finally, if the union must be done with matplotlib artists, I implemented the Cohen-Sutherland clipping algorithm for paths just the other day - I believe clipping one polygon with another is the same as taking their union. I'd be happy to share the code if that is the route you decide to go down (but why would you when you have Shapely!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM