如何在python中以特定角度旋转图形?

时间:2019-07-13 12:02:52

标签: python matplotlib graph

我绘制了一个长方体图形,可以看到below。为了清楚地了解轴,您可以查看this,我想将当前垂直的整个图形旋转为水平的图形。这意味着X和Y轴必须互换。为此...

实际上,一开始我添加了长方体图,并添加了一个矩形补丁作为背景灰色。现在,整个图形应该是横向的,而不是纵向的。我已经看到了您提供的链接。

我的代码类似于下面的情节:

center = [2.10325, -0.100074, 0.76162] ##centre of the body
    length = 0.3 ##defining length, breadth, height
    width = 0.4
    height = 0.1
##defining to plot the cuboid
def cuboid(center, size): 
    """
       Create a data array for cuboid plotting.


       ============= ================================================
       Argument      Description
       ============= ================================================
       center        center of the cuboid, triple
       size          size of the cuboid, triple, (x_width,y_length,z_height)
       :type size: tuple, numpy.array, list
       :param size: size of the cuboid, triple, (x_width,y_length,z_height)
       :type center: tuple, numpy.array, list
       :param center: center of the cuboid, triple, (x,y,z)
   """
    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.scatter(ox,oy,oz,c='crimson',marker='o')  #the previous plot of center 

    ## Add title 
#    plt.title('Plot_for_PSM', fontsize=20)

    ##labelling the axes
#    ax.set_xlabel('X')
#    ax.set_ylabel('Y')
#    ax.set_zlabel('Z')
    ax.add_collection3d(Poly3DCollection(sides, facecolors='black', linewidths=1, edgecolors='black', alpha=.25)) ###This draw the plane sides as requred 
    fig.tight_layout()
#    ax.set_xlim([0,4.5])        
#    ax.set_ylim([-.75,.75])
#    ax.set_zlim([0,1])
   # Hide grid lines 
    ax.grid(False)
#    ax.set_extent([-100, 30, 0, 80], crs=ccrs.PlateCarree())

    # Hide axes ticks
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_zticks([])

    plt.axis('off') #removes the axes from grams
#%%  

输出的方式必须是将肖像模式转换为横向模式。

0 个答案:

没有答案