如何在python中合并来自两个矩形的网格网格点? [英] How to merge mesh grid points from two rectangles in 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
我也可以为蓝色矩形设置网格点.但是,我无法将它们加入单个对象内.我试图将它们作为position1
和position2
的总和加入.我在控制台上得到的值错误为:
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屋!