2013-03-14 91 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時呢?)。