绘制幼体时,3个轴的缩放方式不同

2024-04-27 21:28:24 发布

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

当我绘制一个立方体,三个轴的缩放不同,似乎最长的z轴是最短的,这意味着它的形状发生了变化,看起来很奇怪 the shape I get from python.what I want to get

我要用python绘制一个3d立方体。立方体的轴是a=['6.1290000000000049','0.0000000000000000','0.0000000000000000'],b=['-3.0644999999980','5.307869999948249','0.0000000000000000'],c=['0.0000000000000000000000','0.0000000000000000','13.599999999996]。 如你所见,a和b的长度是相等的,c是最长的,但在我的屏幕上它的变化很奇怪。你知道吗

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
import sys
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
import math

def transPo(l1,l2):
    a = np.array(l1)
    #a = a.T
    b = np.array(l2)
    print(a,b)
    result = np.dot(a, b)
    return result

def lstFloat(lst):
    for i in range(len(lst)):
        lst[i] = float(lst[i])
    return(lst)

def readData():
    with open("INCAR",'r') as f1:
        while  True:
            line = f1.readline()
            if not line:break
            if ("MAGMOM=" in line) and ("#" not in line) :

                mlst=[]
                line=line.strip('MAGMOM=')
                magon = line.strip().split()

                for mag in magon:
                    if "*" in mag:
                        mag = mag.split("*")
                        for n in range(int(mag[0])):
                            mlst.append(float(mag[1]))
                    else:
                        mlst.append(float(mag))


    with open("CONTCAR",'r') as f:

        f.readline()
        f.readline()
        lstLabel=[] # x y z label
        lstPosition=[]
        for i in range(3):
            line = f.readline()
            lstLabel.append(line.strip().split())


        f.readline()
        line = f.readline().strip().split()
        s = 0
        for v in line:
            s += int(v)

        f.readline()

        while True:

            line = f.readline()

            if line ==" \n":
                break
            elif not line:
                break
            else:
                lstPosition.append(line.strip().split())

    lma=[]
    for i in range(s):
        val = [0,0,0]
        val[0] = mlst[i*3]
        val[1] = mlst[i*3 +1]
        val[2] = mlst[i*3 + 2]

        lma.append(val)



    if len(lma)!=len(lstPosition):
        print('error!!! Atom number is not equal to the number of magon\n---------------------------')
        sys.exit()

    print(lstLabel,'\n----lstlabel', lma,'\n---------lma',  lstPosition  )

    return lstLabel, lma, lstPosition




class Arrow3D(FancyArrowPatch):

    def __init__(self, xs, ys, zs, *args, **kwargs):
        FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
        self._verts3d = xs, ys, zs

    def draw(self, renderer):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        FancyArrowPatch.draw(self, renderer)


def main():
    (lstLabel, lma, lstPosition) = readData()

    fig = plt.figure( )
    ax = fig.gca(projection='3d')

    g = np.array([0,0,0])
    print('x',lstFloat(lstLabel[0]))
    x = np.array(lstFloat(lstLabel[0]))
    y = np.array(lstFloat(lstLabel[1]))
    z = np.array(lstFloat(lstLabel[2]))

    x_ = y + z
    y_ = x + z
    z_ = x + y
    g_ = x + y + z

    ax.plot3D(*zip(x,g), color = "r")
    ax.plot3D(*zip(y,g), color = "r")
    ax.plot3D(*zip(z,g), color = "y")
    ax.plot3D(*zip(x_,g_), color = "b")
    ax.plot3D(*zip(y_,g_), color = "b")
    ax.plot3D(*zip(z_,g_), color = "b")
    ax.plot3D(*zip(x_,y), color = "b")
    ax.plot3D(*zip(x_,z), color = "b")
    ax.plot3D(*zip(y_,x), color = "b")
    ax.plot3D(*zip(y_,z), color = "b")
    ax.plot3D(*zip(z_,x), color = "b")
    ax.plot3D(*zip(z_,y), color = "b")
    ax.yaxis.label.set_size(40)

    a = Arrow3D([0, lstLabel[0][0]], [0, lstLabel[0][1]], [0, lstLabel[0][2]], mutation_scale=20,
            lw=1, arrowstyle="-|>", color="k")
    b = Arrow3D([0, lstLabel[1][0]], [0, lstLabel[1][1]], [0, lstLabel[1][2]], mutation_scale=20,
                lw=1, arrowstyle="-|>", color="k")
    c = Arrow3D([0, lstLabel[2][0]], [0, lstLabel[2][1]], [0, lstLabel[2][2]], mutation_scale=20,
                lw=1, arrowstyle="->", color="k")


    ax.scatter([0], [0], [0], color="g", s=100) # yuan dian

    ax.add_artist(a)
    ax.add_artist(b)
    ax.add_artist(c)

    for i in range(len(lma)):

        #lma, lstPosition
        lstFloat(lstPosition[i])

        if (abs(lma[i][0]  == 0 ) and  (abs(lma[i][1]) == 0) and (abs(lma[i][2])  == 0)):
            pass
        else:
            print(np.array(lstPosition[i]),[lstFloat(lstLabel[0]), lstFloat(lstLabel[1]), lstFloat(lstLabel[2])])
            lstPosi = transPo([lstFloat(lstLabel[0]), lstFloat(lstLabel[1]), lstFloat(lstLabel[2])],lstPosition[i])
            print(lstPosi)
            mag = Arrow3D([lstPosi[0] , lstPosi[0] + lma[i][0] ], [lstPosi[1], lstPosi[1] + lma[i][1]], 
                [lstPosi[2], lstPosi[2] + lma[i][2]], mutation_scale=10,
            lw=2, arrowstyle="-|>", color="k")
            ax.add_artist(mag)



    plt.show()

if __name__ == '__main__':
    main()

我想看一个正确的立方体,它的比例是a,b,c


Tags: inimportreadlinenplinezipaxcolor