如何在python中合并来自两个矩形的网格网格点? [英] How to merge mesh grid points from two rectangles in python?

查看:248
本文介绍了如何在python中合并来自两个矩形的网格网格点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并在两个不同(相邻)矩形处形成的网格.以下是矩形的图形表示:

I need to merge the mesh grids formed at two different (adjoining) rectangles. Following is the pictorial representation of the rectangle:

我可以创建各个矩形的网格.例如,对于绿色矩形,使用下面的代码片段,我们可以创建一个网格.

I can create the mesh grids of the individual rectangles. For example for the green rectangle, using following code snippet, we can create a mesh grid.

xvalues = np.array([0, 2, 4, 6, 8, 10])
yvalues = np.array([6, 8, 10, 12])
x, y = np.meshgrid(xvalues, yvalues)
positions = np.vstack([x.ravel(), y.ravel()])
theGridPoints = (np.array(positions)).T

我也可以为蓝色矩形设置网格点.但是,我无法将它们加入单个对象内.我试图将它们作为position1position2的总和加入.我在控制台上得到的值错误为:

I can make the grid points for the blue rectangle too. However, I'm unable to join them inside a single object. I tried to join them as the sum of position1 and position2. I get the value error on the console as:

ValueError: operands could not be broadcast together with shapes (.,.) (.,.)

我该如何解决?

谢谢.

推荐答案

import numpy as np
import matplotlib.pyplot as plt

bx, by = np.mgrid[0:2, 0:5]
gx, gy = np.mgrid[0:10, 5:12]

bp = np.vstack((bx.ravel(), by.ravel()))
gp = np.vstack((gx.ravel(), gy.ravel()))

points = np.hstack((bp, gp)).T

# full grid
plt.scatter(points[:,0], points[:,1], c='orange', s=200)
# green rectangle
plt.scatter(gp.T[:,0], gp.T[:,1], c='green', s=50)
# blue rectangle
plt.scatter(bp.T[:,0], bp.T[:,1], c='blue', s=50)

plt.show()

这篇关于如何在python中合并来自两个矩形的网格网格点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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