3

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()
Moka
  • 31
  • 1
  • 3
  • I have never seen any function in Matplotlib to calculate the union. Matplotlib is intended to visualize data, not to calculate it. After you calculate the shapes of the unions you can use Polygon() to make irregular shapes (instead of Rectangle()). – Robbert Mar 14 '13 at 15:27
  • see http://stackoverflow.com/questions/1517192/whats-a-good-library-to-do-computational-geometry-like-cgal-in-a-garbage-coll – tacaswell Mar 14 '13 at 18:22

1 Answers1

2

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!).

pelson
  • 21,252
  • 4
  • 92
  • 99