如何在python中删除图形绘图中的空格?

2024-09-28 05:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用python用matplolib绘制图形之后,我遇到了一个问题。我得到了这个数字,但是这个数字有我不需要的空白。我已经阅读了stack overflow提供的大部分链接,但是没有一个是关于我的问题的。现在,我想删除空白,并要求与图片的整个图像。你知道吗

实际上,我对python的情节还不熟悉。我已经创造了一个阴谋,其中黑色是框和灰色的颜色是框架。 我使用以下代码创建了3D绘图this 的顶部vie作为图像(.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))


Tags: ofthetonewplotnpfigax

热门问题