0

So,

The data looks like this:

data (the spaces represent different columns, and the data represents 10 particles going around a central particle "Sun" and at different time steps in this case every 1,000 days for 3.6 million days )

and I've been running into trouble trying to make plots for it and I was wondering what I've been doing wrong and a best way to go forward to my ultimate goal of making a 3d animation of the data (x,y,z over time)

I've tried multiple things but I'll just put the simple plot version i was trying yesterday(you'll see some left over variables and importing library's

My code:

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()

here's a link to the file I don't know why it's in this format but here's the whole thing (I'm in a Linux VM and the only way I know how to share this is through gdrive)

https://docs.google.com/spreadsheets/d/1NSgy0laiBW_lAHcXwhuWFq6LhS8oEA2oWeVjjtXQfWI/edit?usp=sharing

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

0

Overview

Here is a good starting point for you. It is a program to plot the positions over time as part of an animation. All it really does is creates lists of coordinates (x,y,z) from the data file. Once the lists are created, you can write a short animate function that plots all points that are present at a given time.

THIS IS A MINIMAL EXAMPLE!!!

There are a lot of directions you could go (updating size/mass of the objects, path tracing, color coding, etc). Have fun!

Helpful links

Animation docs:
https://matplotlib.org/stable/api/animation_api.html

3d scatter plots:
https://matplotlib.org/stable/gallery/mplot3d/scatter3d.html

Scatter animations:
Matplotlib 3D scatter animations

Code

%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()
trent
  • 359
  • 1
  • 9
  • Thank you so much, that's all I needed a good way forward, I'm a physics major who never took a coding class so a lot of my knowledge is scattered. – Jacob Mireles Mar 02 '23 at 18:53
  • No worries! Please accept the answer if it is the one you like!! – trent Mar 03 '23 at 00:17