matplotlib 你将如何在3d中绘制这些数据?(python)

mrwjdhj3  于 2023-03-03  发布在  Python
关注(0)|答案(1)|浏览(193)

所以,
数据如下所示:


(空格表示不同的列,数据表示10个粒子围绕中心粒子"太阳"并且在不同的时间步长,在这种情况下是每1,000天,持续360万天)
我一直遇到麻烦,试图使它的绘图,我想知道我一直在做错什么,以及最好的方式前进,我的最终目标,使数据的三维动画(x,y,z随着时间的推移)
我已经尝试了很多东西,但我只是把简单的绘图版本,我昨天尝试(你会看到一些剩余的变量和导入库的
我的代码:

import numpy as np
import math
import csv
import matplotlib.pyplot as plt

from random import randint
import matplotlib.animation 
from matplotlib.animation import FuncAnimation
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
from mpl_toolkits import mplot3d

import pandas as pd

arc = open('data10id_1S.csv', 'r')
lines = arc.readlines()

a = []

newList = []
for i in range(0, len(lines)):
    if lines[i][:2] == 't=':
    
        if i != 0:
            newList = newList + [a]
            s = lines[i].replace(',','').split()
            t = float(s[1]) 
    

    else:
        s = lines[i].replace(',','').split()
        #  storing the particles data
        data = pd.DataFrame[float(s[1]), 
            float(s[3]), 
            float(s[9]), 
            float(s[5]), 
            float(s[11]), 
            float(s[7]), 
            float(s[13])]
         a = [data]

        # setting the particels variables
        m = float(s[1])
        x = float(s[3])
        vx = float(s[9])
        y = float(s[5])
        vy = float(s[11])
        z = float(s[7])
        vz = float(s[13])
        e = float(s[15])
        axis = float(s[17])
        a = a + [data]

        # setting the central particles variables
        px = a[0][1]
        pvx = a[0][2]
        py = a[0][3]
        pvy = a[0][4]
        pz = a[0][5]
        pvz = a[0][6]
        pm = a[0][0]
      fig = plt.figure()
      ax = p3.Axes3D(fig)


         lines = ax.plot(x,y,z)

         ax.set_xlim3d([-20.0, 20.0])
         ax.set_xlabel('x')

         ax.set_ylim3d([-20.0, 20.0])
         ax.set_ylabel('Y')

         ax.set_zlim3d([-20.0, 20.0])
         ax.set_zlabel('Z')
         ax.set_title("3D Plot over Time")
        
         line_ani = animation.FuncAnimation(fig, t, 3652500, fargs=(data, lines), interval=10, blit=True)
        
         plt.show()

这里有一个文件的链接我不知道为什么它是这种格式,但这里是整个事情(我在一个Linux虚拟机,我知道如何共享它的唯一方式是通过gdrive)
https://docs.google.com/spreadsheets/d/1NSgy0laiBW_lAHcXwhuWFq6LhS8oEA2oWeVjjtXQfWI/edit?usp=sharing

ijxebb2r

ijxebb2r1#

概述

这是一个很好的起点。这是一个绘制位置随时间变化的程序,作为动画的一部分。它真正做的是从数据文件创建坐标(x,y,z)列表。一旦列表创建好,你就可以编写一个简短的animate函数来绘制给定时间出现的所有点。
这是一个最小的例子!
有很多方向你可以去(更新大小/质量的对象,路径跟踪,颜色编码等)。玩得开心!

有用链接

动画文档:
https://matplotlib.org/stable/api/animation_api.html
3d散点图:
https://matplotlib.org/stable/gallery/mplot3d/scatter3d.html
散布动画:
Matplotlib 3D scatter animations

代码

%matplotlib notebook
import numpy as np
import math
import csv
import matplotlib.pyplot as plt

from random import randint
import matplotlib.animation 
from matplotlib.animation import FuncAnimation
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
from mpl_toolkits import mplot3d

import pandas as pd

arc = open('data10id_1S.csv', 'r')
lines = arc.readlines()

xList = []
yList = []
zList = []
j = 1
while j < len(lines) -8:
    tempX = []
    tempY = []
    tempZ = []
    while (lines[j][:2] != 't='):
        s = lines[j].replace(',',' ').split()
        #  storing the particles locations
        tempX.append(float(s[3]))
        tempY.append(float(s[5]))
        tempZ.append(float(s[7]))
        j += 1
    j+=1   
    xList.append(tempX)
    yList.append(tempY)
    zList.append(tempZ)

fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')

ax.set_xlim3d([-5.0, 5.0])
ax.set_xlabel('x')

ax.set_ylim3d([-5.0, 5.0])
ax.set_ylabel('Y')

ax.set_zlim3d([-5.0, 5.0])
ax.set_zlabel('Z')
ax.set_title("3D Plot over Time")

pts = ax.scatter(xList[0], yList[0], zList[0], s=30)

def animate(i):
    # setting data to new scatter coordinates
    pts._offsets3d = (xList[i], yList[i], zList[i])

    return pts

anim = animation.FuncAnimation(fig, animate, frames = len(xList), interval=5, repeat=False, blit=False)

plt.show()

相关问题