Rotating Solar System Simulation in Python

This document provides the instructions and Python code to create a simple rotating solar system simulation. The simulation includes multiple planets orbiting around the sun with different sizes, speeds, and orbital paths. It uses the matplotlib library's animation module to create the orbital paths and simulate movement over time.

Requirements

Before running the program, make sure you have matplotlib installed. You can install it with the following command:

pip install matplotlib

Python Code

Here's the Python code for the solar system simulation. It uses matplotlib to create an animation of planets orbiting around the sun. Each planet is represented as a circle with specific properties like distance from the sun, radius, speed, and color.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Set up the figure and axis
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
ax.axis('off')  # Turn off axis lines and labels

# Sun's properties
sun_radius = 0.1
sun = plt.Circle((0, 0), sun_radius, color='yellow', zorder=10)
ax.add_artist(sun)

# Define planet properties (distance from sun, radius, orbital speed)
# Each tuple is (distance, radius, speed in radians per frame)
planets = [
    {"distance": 0.3, "radius": 0.05, "speed": 0.03, "color": "blue"},
    {"distance": 0.6, "radius": 0.07, "speed": 0.02, "color": "red"},
    {"distance": 0.9, "radius": 0.09, "speed": 0.01, "color": "green"}
]

# Create planet artists and add them to the plot
planet_artists = []
for planet in planets:
    circle = plt.Circle((planet["distance"], 0), planet["radius"], color=planet["color"], zorder=5)
    ax.add_artist(circle)
    planet_artists.append(circle)

# Initialize animation angles for each planet
angles = [0 for _ in planets]

# Animation function
def update(frame):
    global angles
    for i, planet in enumerate(planets):
        # Update each planet's position based on its speed and distance
        angles[i] += planet["speed"]
        x = planet["distance"] * np.cos(angles[i])
        y = planet["distance"] * np.sin(angles[i])
        planet_artists[i].set_center((x, y))
    return planet_artists

# Create the animation
ani = animation.FuncAnimation(fig, update, frames=360, interval=20, blit=True)

# Show the animation
plt.show()

Explanation of the Code

Required files:
• PX_lastname_Solar.py (Python code)
• PX_lastname_Solar.png (Screenshot of output)
• PX_lastname_Solar.mp4 (Video of running program)

Submit all files to Google Classroom.