Reputation: 858
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 the 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
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 (.,.) (.,.)
How can I solve it?
Upvotes: 1
Views: 3461
Reputation: 5774
If you want to join/merge multiple np.ndarray
, they need to be of the same shape
. So you the number of xvalues
of the blue rectangle need to be equal to the number of xvalues
of the green rectangle. Same for the yvalues
.
Here is a short example:
xvalues_b = np.array([0, 1, 2, 3, 4, 5])
yvalues_b = np.array([0, 1*5/3, 2*5/3, 3*5/3])
x_b, y_b = np.meshgrid(xvalues_b, yvalues_b)
positions_b = np.vstack([x_b.ravel(), y_b.ravel()])
theGridPoints_b = (np.array(positions_b)).T
positions_gb = np.concatenate([positions, positions_b])
Upvotes: 1
Reputation: 5294
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()
Upvotes: 2