Bringing You The Future Of Science and Technology

SOLAR SYSTEM

In this tutorial we take a closer look at the solar system using Python and Matplotlib, to create a scale model of the solar system. In this tutorial, we will show the size, position and orbits of each planet.

Latest Articles

The Solar System

Since the dawn of mankind we have look towards the stars with awe and wonder. Over the centuries, astronomy-priests have identified and tracked various celestial bodies that surround our world in a better attempt to understand the universe. Exploring our solar system gives us insight into the formation of our own planet and a possible view into the possible future of our planet. Everything from the Sun, asteroids and comets, to dwarf planets, terrestrial planets and gas giants, have been explored and charted.

Before we begin coding our script, we need to collect the necessary planetary data. Most of the following data was obtained using the NASA Planetary Fact Sheet.

In this tutorial, all calculations will be done using the metric system. In some cases data is available in feet or miles (e.g. the Imperial system) and must be converted to the meters or kilometers (the metric system). If you are using the NASA Planetary Fact Sheet, don't forget to convert from diameter to radius for each celestial body by using the following code:

Planetary Data

Planetary statistics can be found in any planetary science book or good physics book, but the internet is also a good place to search for planetary data. NASA offers planetary facts in metric and standard formats. We use the metric system for all scientific computing.

Planetary Fact Sheet

Planetary Body

Mass (kg)

Radius (km)

Orbital Period (days)

Rotation Period (hrs)

Inclination (deg)

Eccentricity

Velocity (km/s)

Mean Surface Temp (C)

Distance from Sun (km)

Mercury

3.30 x 1023

2439

87.98

58.65

0

0.205

47.4

430

580 x 106

Venus

4.87 x 1024

6051

224.7

5832.24

117.4

0.007

35

480

108 x 106

Earth

5.98 x 1024

6378

365.36

24

23.5

0.017

29.8

22

150 x 106

Mars

0.64 x 1023

3393

686.98

24.56

23.98

0.094

24

-23

228 x 106

Jupiter

1.90 x 1027

71492

11.86

9.89

3.1

0.049

13.1

-150

778 x 106

Saturn

5.69 x 1026

60268

29.41

10.24

27

0.057

9.7

-80

1432 x 106

Uranus

8.68 x 1025

25559

84.04

17.25

97.9

0.046

6.8

-215

2871 x 106

Neptune

1.02 x 1026

24764

164.8

16.10

29.6

0.011

5.4

-220

4498 x 106

Sun

1.99 x 1030

696000

***

24.47

-

-

220

5600

-

Moon

7.35 x 1022

1738

27

648

6.7

0.055

1

127 (-173)

**

*** Based on a distance of 30,000 light years, and travelling at a speed of 220 km/s, the Sun takes approximately 225 million years to orbit around the Milky Way.
** The Moon's average distance to Earth is 384,400 km (238,900 mi).
- Missing or incomplete data.

We will save the above planetary data as series of lists, multi-dimensional arrays or dictionaries. For our purposes, we will create lists of intergers, strings and lists of variables. These lists can then be used to by our script, printed to the screen, or plotted in bar or scatter charts. We will learn how this data can be visualized using the popular Matplotlib library. To install Matplotlib on Debian/Unix systems, open a terminal window and enter the following command:

$ sudo apt-get install python-matplotlib

To install Matplotlib on a Windows operating system, open the command prompt and enter the following commands:

Matplotlib will be used to plot various planetary features, such as the average distance of each planet from the Sun, orbital periods and more. In order to use Matplotlib in our script, we must first import it. Begin by opening a python shell. To enter the Python shell in Windows command prompt or Unix terminal, enter the following command:

$ python
>>>

You can test the Python shell by entering a simple equation. For example, you could enter the following:

>>> 12*12
144

Begin by creating a list of strings, named planets to hold the names of each planet (to include the moon and Sun). A list is an ordered collection of values. In Python, a list is similar to what other languages call an array, but with some added functionality. Lists can contain any type of data, from strings, intergers and floating point numbers. The items that make of a list are simply called items, or elements.

We can reference each planetary body by referencing it's position in the list.

>>> print("The planet", planets[0], " is closest to the Sun")
The planet Mercury is closest to the Sun

First, we will identify the planetary variables for each individual planet (such as the mass, radius, density and volume), and then use these individual variables to create lists of the collected variables. Notice that some of these parameters are very small or very large and therefore are presented using the scientific notation. In Python, the ** operator is used to raise the number on the left to the power of the exponent on the right. For example, to raise 5 to the power of 3 (53), that is to multiply 5 by itself 3 times, we use the following expression:

>>> 5**3
125

When listing the following planetary variables, it is important to list each variable in the exact order as in the list of planets above. This allows us to programatically call upon each element in the list by accessing it's order in the list. Recall that elements in a list begin at 0, rather than 1. For example, in order to access the planet Mars from the above list you could use:

>>> planets[3]
'Mars'

We will begin by including the planetary mass of each planet, to include the moon and Sun. Planetary mass is a measure of the mass of each planet and is measured in kilograms (kg). The planetary masses are measured in septilion (1 followed by 24 zeros) kilograms. As you will notice, the planetary masses are listed in scientific notation and must be calculated to get the actual numbers for each planetary mass.

Once these parameters are calculated, they can be added to a list of planetary masses (more specifically, we will create a 2-dimensional array, where each element of the array is a variable pointing to the actual value), so that we can then calculate the gravitational attraction and escape velocity for each planetary body.

We could just use the above code, but we prefer to work with clean data. So we can simply print the list of planetary masses, then copy the results and replace the variables in the above list (mass_list) with the actual values.

Next, we will calculate the radius for each planet, based on it's diameter (NASA Planetary Fact Sheet provides the diameter of each planetary body in kilometers). For our purposes, we are only interested in the radius of each planet, as this will be used when determining the gravitational forces acting on each planet. Also, we will want to convert these numbers from kilometers to meters before using them.

If you are using the Planetary Fact Sheet displayed at the top of the page, the data has been converted for you, however, if you are using the NASA Planetary Fact Sheet, then you may want to use the following code to convert from diameter to radius and from kilometers to meters:

Next we will calculate the volume of each planetary body. A planet's volume tells us how much space it occupies in space. To calulate the volume for each planetary body, we will use the following equation:

V = 4/3 π r3

Before we begin calculating the volume for each planetary body, we need to convert the radius_list from meters to kilometers. We will do this by dividing each element in the radius_list by 103.

The average density of each planetary body is measured in kilograms per cubic meter. Dividing mass and volume we gives us the density of each planetary body using the following equation:

ρ = m / V

Before proceeding, we must again scale down the mass of each planetary body. Once the masses are in the correct scale, we can calculate the density of each planet. Again, these calculations are compared to the NASA Planetary Fact Sheets linked above.

Orbital Parameters

In the next section, we will list the semimajor axis, or average distance from the Sun, of each planetary body in kilometers. Keep in mind, all planets follow an elliptical orbit, rather than a perfectly circular one. The point in an orbit when the planet is farthest from the Sun is known as aphelion, likewise, when the planet is closest to the Sun it is known as perihelion. We can also plot these distances, but for our solar system model we will only use the average distances.

Other orbital parameters of interest to scientists include the orbital period of each planetary body. The orbital period is the time in Earth days for a planet to orbit the Sun from one vernal equinox to the next. This is also known as the tropical orbit period. We will use the orbital periods provided by NASA (link above).

The speed at which a planetary body travels around the Sun is known as it's orbital velocity, and is measured in kilometers per second. To calculate the orbit of each planet around the Sun, all we need to know is the average distance of the planet from the Sun and the time it takes for the planet to complete one revolution around the Sun. To calculate the planet's orbital velocity we will use the following equation:

We could use the above equation to calculate the velocity of an orbiting satellite or spacecraft, such as the International Space Station. For the International Space Station (ISS), we will collect some more data from NASA. For the radius (r) we will use the radius of the Earth (6378.8 km) plus the altitude of the ISS (408 km). The orbital period of the ISS is 92.49 minutes, which needs to be converted to seconds. Using the above equation we can easily calculate the orbital velocity of the ISS in Python.

We can confirm with the European Space Agency (ESA) that our calculations are correct. The average orbital velocity of the ISS, according to the ESA, is approximately 7.6 - 7.7 km/s.

Statistical Analysis

We can use Python and Matplotlib to create visualizations to help us beter understand the relationships between the planets and the Sun. For example, we could show the distances of each planet from the Sun.

Looking at the above bar graph allows us to easily visualize the vast distances between planets. Upon closer inspection, we begin to see a pattern to the distances between the planets. It appears that a planet is missing in the spot where the Asteroid Belt is located, between Mars and Jupiter. In the next section of this tutorial we will explore different ways to calculate the weight of a spacecraft or other object on each planetary body.

Scale Model of the Solar System

The following Python script uses Matplotlib to draw the entire solar system, to scale. I have chosen the date of September 4, 2018 for the approximate placement of the planets in their orbits. I added the orbits for each planet and labels to help identify each planet.