How can I change the starting values of a scientific model to get a second line using MatPlotLib?

I’ve just recently starting using Python for a school project, and I am trying to create multiple lines in a graph that are calculated using different base values. The model calculates the orbit of a satelite and the graph has the time (in seconds) on the x-axis and the height (in meters) on the y-axis. Is it possible to retain the line from the first run of the model or do I have to copy and paste it with a new data label? Thanks in advance for the help.

from typing import Any, Union
import matplotlib.pyplot as plt
import matplotlib.animation as ani

data = []
t = 0
dt = 10

A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)

r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5

vx = v
vy = 0
Px = 0
Py = r

while 0 < h < h0 + 100000:
    # for _ in range(2592000):
    t = t + dt

    r = (Px ** 2 + Py ** 2) ** .5
    Fg = G * M * m / r ** 2
    Fgx = -Fg * Px / r
    Fgy = -Fg * Py / r

    h = r - 6371000
    if h > 600000:
        z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
    else:
        if h > 139000:
            z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
        else:
            z = 1.225 * 0.863697 ** (h / 1000)
    v = (vx ** 2 + vy ** 2) ** .5
    Fwl = 0.5 * z * Cw * A * v ** 2
    Fwlx = -Fwl * vx / v
    Fwly = -Fwl * vy / v

    ax = (Fgx + Fwlx) / m
    ay = (Fgy + Fwly) / m
    vx = vx + ax * dt
    vy = vy + ay * dt
    Px = Px + vx * dt
    Py = Py + vy * dt

    data += [[t, h]]

fig, ax = plt.subplots()
ax.plot(*zip(*data))
ax.set_xlabel('Tijd (s)')
ax.set_ylabel('Hoogte (m)')
ax.set_title("Hoogte satelliet vanaf 300km")
plt.show()

Answer

Yes, of course you can.
A possible way is to save data you computed in data in a .csv file.
One of the most popular way to handle with .csv file is pandas library.
So you should:

  • convert data list into a numpy.array data_to_save
  • save first column of data_to_save as 'time' column of a pandas.DataFrame, and second column of data_to_save as 'height' column of the same dataframe df
  • save df dataframe to .csv file

Working code

import pandas as pd
import numpy as np

# here you compute data

data_to_save = np.array(data)

df = pd.DataFrame({'time': data_to_save[:, 0],
                   'height': data_to_save[:, 1]})

df.to_csv('my_data.csv', index = False)

This will create a my_data.csv where first lines are:

time,height
10.0,300000.0
20.0,299552.2454725085
30.0,299104.5811199909
40.0,298657.0670737019
50.0,298209.7634687573
60.0,297762.7304360224
70.0,297316.02809399646
80.0,296869.7165406905

In a second step, you can recall your data in a python script by reading that .csv file with:

import pandas as pd

df = pd.read_csv('my_data.csv')

And you can access 'time' values with df['time'] or df.time and 'height' values with df['height'] or df.height.