How to meshgrid this non-rectangular shape (a trapezoid) – to be used for 3d volumteric visualtion – python
Question:
Im trying to create a volume with a custom shape, to add Z-axis data I need to mesh X-Y data.
Hence my issue. I’d like to have this shape as the base
Trapezoid Base
However, after doing
X,Y = np.mesh(x,y)
I get a symmetric rectangle rather than the trapezoid-looking like shape.
meshed data
is there another effecient way I can fill the trapezoid ?
here is the code:
x1 = np.zeros(20)
y1 = np.linspace(-2,2,20)
x2 = np.linspace(0,30,20)
y2 = np.sqrt(( 5/max(x2) * x2 +4 ))
x3 = np.linspace(0, 30, 20)
y3 = -np.sqrt((5 / max(x3) * x3 + 4))
x4 = np.ones(20)*30
y4 = np.linspace(-3,3,20)
x = np.concatenate((x1, x2, x3, x4))
y = np.concatenate((y1,y2,y3,y4))
# u,v = np.meshgrid(x,y)
# x = u.flatten()
# y = v.flatten()
plt.plot(x,y)
plt.show()
Trying to fill a trapizoid shape to construct a 3d volume representation using plotly
Answers:
I think you can do something like this.
The key is to define a normalized version of y
that has a domain of -1 to 1 and multiply it by your final equation for y
import numpy as np
import matplotlib.pyplot as plt
n = 20
x = np.linspace(0, 30, n)
y = np.linspace(-1, 1, n)
xx, yy = np.meshgrid(x, y)
yy *= np.sqrt(5/x.max() * x + 4)
plt.scatter(xx, yy)
plt.show()
Im trying to create a volume with a custom shape, to add Z-axis data I need to mesh X-Y data.
Hence my issue. I’d like to have this shape as the base
Trapezoid Base
However, after doing
X,Y = np.mesh(x,y)
I get a symmetric rectangle rather than the trapezoid-looking like shape.
meshed data
is there another effecient way I can fill the trapezoid ?
here is the code:
x1 = np.zeros(20)
y1 = np.linspace(-2,2,20)
x2 = np.linspace(0,30,20)
y2 = np.sqrt(( 5/max(x2) * x2 +4 ))
x3 = np.linspace(0, 30, 20)
y3 = -np.sqrt((5 / max(x3) * x3 + 4))
x4 = np.ones(20)*30
y4 = np.linspace(-3,3,20)
x = np.concatenate((x1, x2, x3, x4))
y = np.concatenate((y1,y2,y3,y4))
# u,v = np.meshgrid(x,y)
# x = u.flatten()
# y = v.flatten()
plt.plot(x,y)
plt.show()
Trying to fill a trapizoid shape to construct a 3d volume representation using plotly
I think you can do something like this.
The key is to define a normalized version of y
that has a domain of -1 to 1 and multiply it by your final equation for y
import numpy as np
import matplotlib.pyplot as plt
n = 20
x = np.linspace(0, 30, n)
y = np.linspace(-1, 1, n)
xx, yy = np.meshgrid(x, y)
yy *= np.sqrt(5/x.max() * x + 4)
plt.scatter(xx, yy)
plt.show()