2013-03-14 96 views
2

我正在使用Matplotlib和Python。我想绘制一组矩形的联合。 矩形可以连接或断开。我还希望为与其他组共享的边赋予不同的颜色,以知道组之间不存在重叠区域。你有什么主意吗?如何在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() 
+0

我从来没有在Matplotlib中看到任何函数来计算联合。 Matplotlib旨在可视化数据,而不是计算它。计算组合的形状后,可以使用Polygon()制作不规则形状(而不是Rectangle())。 – Robbert 2013-03-14 15:27:35

+0

请参阅http://stackoverflow.com/questions/1517192/whats-a-good-library-to-do-computational-geometry-like-cgal-in-a-garbage-coll – tacaswell 2013-03-14 18:22:51

回答

1

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

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

最后,如果工会必须与matplotlib艺术家来完成,我实现了Cohen-Sutherland clipping algorithm的路径只是有一天 - 我相信,削减与另一个多边形相同服用他们的结合。如果那是你决定走下坡路的话,我会很高兴地分享这些代码(但是为什么当你有Shapely时呢?)。