gpt4 book ai didi

python - 如何在python中合并两个矩形的网格点?

转载 作者:行者123 更新时间:2023-11-30 22:15:18 25 4
gpt4 key购买 nike

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

enter image description here

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

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 的总和加入。我在控制台上得到的值错误为:

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

enter image description here

关于python - 如何在python中合并两个矩形的网格点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50330251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com