使用python用matplolib绘制图形后,我遇到了问题。我知道了这个数字,但是这个数字有我不需要的空白。我已经阅读了堆栈溢出提供的大多数链接,但是都没有涉及到我的问题。现在,我要删除空格,并要求整个图片都带有图片。
实际上,我是python中的新手。我创建了一个图,黑色是方框,灰色是框架。 我使用以下代码将3D图this 的顶部图像创建为图像(.png)。
def cuboid(center, size):
ox, oy, oz = center
l, w, h = size
###Added the fig in order to be able to plot it later
ax = fig.gca(projection='3d') ##plot the project cuboid
X=[ox-l/2,ox-l/2,ox-l/2,ox-l/2,ox+l/2,ox+l/2,ox+l/2,ox+l/2] ##corner points of the cuboid
Y=[oy+w/2,oy-w/2,oy-w/2,oy+w/2,oy+w/2,oy-w/2,oy-w/2,oy+w/2]
Z=[oz-h/2,oz-h/2,oz+h/2,oz+h/2,oz+h/2,oz+h/2,oz-h/2,oz-h/2]
# ax.scatter(X,Y,Z,c='g',marker='o') #the plot before rotated
X_new = ([]) #attaining new corner points after rotated
Y_new = ([])
Z_new = ([])
for i in range(0,8):
c=np.matrix([[X[i]], ##reading every corner points into matrix format
[Y[i]],
[Z[i]]])
u=Rot_Mat*c ##rotating every corner point with the rotation matrix
X_new = np.append(X_new, u.item(0)) ##appending the corner points with the neighbours
Y_new = np.append(Y_new, u.item(1))
Z_new = np.append(Z_new, u.item(2))
print('\nvertex=\n',c)
print('\nnew_vertex=\n',u)
###Doing a dot product between Rot_Mat and c as earlier but using np.dot as it is necessary with Numpy format, reshaping from(3,1) to (3)
side[i,:] = np.dot(Rot_Mat, c).reshape(3)
sides = [[side[0],side[1],side[2],side[3]], ##defining the 6 sides of cuboid
[side[4],side[5],side[6],side[7]],
[side[0],side[1],side[4],side[5]],
[side[2],side[3],side[4],side[5]],
[side[1],side[2],side[5],side[6]],
[side[4],side[7],side[0],side[3]]]
ax.scatter(X_new,Y_new,Z_new,c='blue',marker='') #the plot of corner points after rotated
ax.add_collection3d(Poly3DCollection(sides, facecolors='black', linewidths=1, edgecolors='black', alpha=.25)) ###This draw the plane sides as requred
fig.tight_layout()
# Hide grid lines
ax.grid(False)
# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
plt.axis('off') #removes the axes from grams
创建此长方体图的初始化数据如下:
fig=plt.figure(figsize=(6,6)) ##to obtain figure and dimensions of graph
ax = fig.add_axes([0,0,1,1], projection='3d')
#plot planes
p = Rectangle((0,-0.7), 4.5,1.4, color="lightgrey", alpha=0.2) #plots the background frame
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
i=pd.read_excel('Bond0.dump.xlsx') ##to read the excel file format
X=i['x'] ## to import the variable on to axes from data set
Y=i['y']
Z=i['z']
j=pd.read_excel('paketone4000.dump.xlsx') ##to read the excel file format
X=j['x'] ## to import the variable on to axes from data set
Y=j['y']
Z=j['z']
a=j['x']##import centre of mass from excel file format
b=j['y']
c=j['z']
#cuboid initialising parameters
center = [a[0], b[0], c[0]] ##centre of the body
length = 0.3 ##defining length, breadth, height
width = 0.4
height = 0.1
side = np.zeros((8,3)) ###This numpy vector will be used to store the position of the sides
预期结果是我必须去除图片中的空白并形成带有灰色框(垂直尺寸=(0,4.5),水平尺寸=(-0.7,0.7))的图片