Issue
I am tring to plot a cube on top of a plane using matplotib.
I am doing a project on UAV(An unmanned aerial vehicle) Landing and there are these tests i have to visualize. I have a landing plane already plotted as shown below.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
xs = np.linspace(-10, 10, 100)
ys = np.linspace(-10, 20, 100)
X, Y = np.meshgrid(xs, zs)
Z = 0 / X
fig = plt.figure(figsize=(25,25))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_ylim([-40, 40])
ax.set_zlim([0, 10])
ax.set_xlim([-10, 10])
# Hide grid lines
# ax.grid(False)
plt.xlabel("X axis")
plt.ylabel("Y axis")
# Hide axes ticks
# ax.set_xticks([])
# ax.set_yticks([])
# ax.set_zticks([])
# plt.savefig("foo.png")
plt.show()
and the output is the following image.
now i want to plot a cube inside the figure. I have a function that generates all the vertices points of a cube but i am running into errors trying to plot it. The vertices are generated from top-Northwest,top-NorthEast,top-Southeast, top-south west. the same applies for bottom.
Here is the function.
def createVertices(c3, stepSize):
hs = 0.5 * stepSize
a = [c3[0]-hs,c3[1]+hs,c3[2]+hs]
b = [c3[0]+hs,c3[1]+hs,c3[2]+hs]
c = [c3[0]+hs,c3[1]-hs,c3[2]+hs]
d = [c3[0]-hs,c3[1]-hs,c3[2]+hs]
e = [c3[0]-hs,c3[1]+hs,c3[2]-hs]
f = [c3[0]+hs,c3[1]+hs,c3[2]-hs]
g = [c3[0]+hs,c3[1]-hs,c3[2]-hs]
h = [c3[0]-hs,c3[1]-hs,c3[2]-hs]
return [a,b,c,d,e,f,g,h]
I have to do this dynamically as I have over 100 testcases. Any help will be much appreciated.
ADDITIONAL INFORMATION:
Jared suggested almost similar question but I run into an error AttributeError: 'int' object has no attribute 'ndim'
coming from ax.plot_surface(X,Y,1, alpha=0.5)
.
Solution
Since the Matplotlib third-party package, S3Dlib, has predefined cubes, just using that to get:
where the code is:
import matplotlib.pyplot as plt
import s3dlib.surface as s3d
# test cases ......
stepSize = [ 3, 6, 1, 4 ]
centers = [ [5,20,6] , [-7,7,7] , [1,1,1], [0,-30,5] ]
colors = [ 'C0', 'C1', 'C2', 'C4']
shade=False #...................... set True to show solid cubes
for i, ss in enumerate(stepSize) :
surf = s3d.CubicSurface(0,color=colors[i]).domain( ss/2, ss/2, ss/2 )
surf.transform(translate=centers[i])
surf = surf.shade() if shade else surf.edges
boxes = surf if i==0 else boxes+surf
landing = s3d.PlanarSurface(0,color='.6').domain(10,10)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set( xlim = [-10,10], ylim=[-40,40], zlim=[0,10],
xlabel='X', ylabel='Y', zlabel='Z' )
ax.add_collection3d(landing)
ax.add_collection3d(boxes)
fig.tight_layout()
plt.show()
It appears from the createVertices
function, the cube center coordinates and widths are passed in, so just directly used those.
Answered By - Dr.Z
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.