3

我正在使用 Matplotlib 和 Python。我想绘制一组矩形的并集。矩形可以连接或断开。我还想为与其他组共同的侧面分配不同的颜色,因为我知道组之间没有重叠区域。你有什么主意吗?

谢谢你的帮助。

我添加了更精确的代码,我尝试为每组矩形制作一个集合并给它们相同的边缘颜色,但是如何只获得一个形状(矩形组的周长)?

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()
4

1 回答 1

2

看看Shapely。有一个明确的联合示例http://toblerity.github.com/shapely/manual.html#object.union

要绘制 Shapely 几何图形,您可能还需要使用https://pypi.python.org/pypi/descartes

最后,如果联合必须与 matplotlib 艺术家一起完成,我前几天为路径实现了Cohen-Sutherland 裁剪算法- 我相信用另一个多边形裁剪一个多边形与采用它们的联合相同。如果这是您决定走的路线,我很乐意分享代码(但是当您拥有 Shapely 时,您为什么要分享!)。

于 2013-03-14T22:12:33.850 回答