Skip to content

Instantly share code, notes, and snippets.

@fredzannarbor
Created May 7, 2025 00:10
Show Gist options
  • Save fredzannarbor/c362cd187f20de76b2b663f318501419 to your computer and use it in GitHub Desktop.
Save fredzannarbor/c362cd187f20de76b2b663f318501419 to your computer and use it in GitHub Desktop.
script
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Sample data: [l (deg), b (deg), distance (parsecs), name]
data = [
[0.0, 0.0, 0.0, "Earth (Sun)"],
[313.94, -1.91, 1.30, "Proxima Centauri"],
[315.73, -0.68, 1.34, "Alpha Centauri A"],
[31.01, -14.07, 1.83, "Barnard's Star"],
[227.23, -8.89, 2.64, "Sirius A"],
[195.67, -48.05, 3.22, "Epsilon Eridani"]
]
# Extract columns
l = np.array([row[0] for row in data]) # Galactic longitude (degrees)
b = np.array([row[1] for row in data]) # Galactic latitude (degrees)
d = np.array([row[2] for row in data]) # Distance (parsecs)
names = [row[3] for row in data] # Object names
# Convert to radians
l_rad = np.radians(l)
b_rad = np.radians(b)
# Convert to Cartesian coordinates
x = d * np.cos(b_rad) * np.cos(l_rad)
y = d * np.cos(b_rad) * np.sin(l_rad)
z = d * np.sin(b_rad)
# Apply a scaling factor to increase spacing among stars
scaling_factor = 2.0 # Adjust this value to increase or decrease spacing
x = x * scaling_factor
y = y * scaling_factor
z = z * scaling_factor
# Create 3D scatter plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot stars
scatter = ax.scatter(x, y, z, c='blue', s=50, label='Stars')
ax.scatter(0, 0, 0, c='red', s=100, label='Earth (Sun)') # Highlight Earth
# Annotate points with names, with custom offsets to avoid overlap
for i, name in enumerate(names):
# Default position is at the star's coordinates
x_pos, y_pos, z_pos = x[i], y[i], z[i]
# Apply custom offsets for specific stars to prevent overlap
if name == "Proxima Centauri":
x_pos += 0.2 # Shift right in x-direction
y_pos += 0.1 # Shift up in y-direction
elif name == "Alpha Centauri A":
x_pos -= 0.2 # Shift left in x-direction
y_pos -= 0.7 # Shift down in y-direction
# Add the text label at the adjusted position
ax.text(x_pos, y_pos, z_pos, name, fontsize=10)
# Set labels
ax.set_xlabel('X (parsecs)')
ax.set_ylabel('Y (parsecs)')
ax.set_zlabel('Z (parsecs)')
ax.set_title('3D Map of Nearby Stars in Galactic Coordinates')
# Set equal aspect ratio (approximate)
ax.set_box_aspect([1, 1, 1])
# Add legend
ax.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment