如何在python中绘制矩形的并集形状 [英] How to draw the union shape of rectangles in python

查看:68
本文介绍了如何在python中绘制矩形的并集形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Matplotlib和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?

感谢您的帮助.

我添加了更高的代码精度,我试图为每组矩形收集一个颜色并赋予它们相同的边缘颜色,但是如何仅获得一个形状(矩形组的周长)?

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

推荐答案

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

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

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

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

最后,如果必须由matplotlib艺术家完成工会,我将实施Cohen-Sutherland 裁剪算法 前几天用于路径 - 我相信用另一个裁剪一个多边形与采用它们的并集相同.如果您决定沿途走那条路,我将很乐意与您分享代码(但是,当您拥有Shapely时,为什么还要这么做!).

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

这篇关于如何在python中绘制矩形的并集形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆