ibrahim yıldız commited on
Commit
782cdcc
·
verified ·
1 Parent(s): 938fef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -105
app.py CHANGED
@@ -3,130 +3,134 @@ import math
3
  import plotly.graph_objects as go
4
  import streamlit as st
5
 
6
- # Function to create spheres
7
  def spheres(size, clr, dist=0):
8
- theta = np.linspace(0, 2*np.pi, 100)
9
- phi = np.linspace(0, np.pi, 100)
10
- x0 = dist + size * np.outer(np.cos(theta), np.sin(phi))
11
- y0 = size * np.outer(np.sin(theta), np.sin(phi))
12
- z0 = size * np.outer(np.ones(100), np.cos(phi))
13
- trace = go.Surface(x=x0, y=y0, z=z0, colorscale=[[0, clr], [1, clr]])
 
 
 
14
  trace.update(showscale=False)
15
  return trace
16
 
17
- # Function to create orbits
18
  def orbits(dist, offset=0, clr='white', wdth=2):
19
- xcrd = []
20
- ycrd = []
21
- zcrd = []
22
- for i in range(0, 361):
23
- xcrd.append((round(np.cos(math.radians(i)), 5)) * dist + offset)
24
- ycrd.append((round(np.sin(math.radians(i)), 5)) * dist)
25
- zcrd.append(0)
26
- trace = go.Scatter3d(x=xcrd, y=ycrd, z=zcrd, marker=dict(size=0.1), line=dict(color=clr, width=wdth))
 
 
27
  return trace
28
 
29
- # Function to define annotations
30
  def annot(xcrd, zcrd, txt, xancr='center'):
31
- strng = dict(showarrow=False, x=xcrd, y=0, z=zcrd, text=txt, xanchor=xancr, font=dict(color='#BEBEBE', size=12))
 
 
32
  return strng
33
 
34
  # The Streamlit app
35
  def main():
36
- st.title('Solar System Visualization')
37
- st.write('##### Zoom in to realize how small and insignificant you are!')
38
 
39
- # VISUALIZE THE SOLAR SYSTEM
40
- # Diameters in km
41
- diameter_km = [1392700, 4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528]
42
- # Distances from the Sun in millions of km
43
- distance_from_sun = [0, 57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1]
44
- # Eccentricity and inclination values (in degrees) for each planet's orbit
45
- eccentricities = [0, 0.205, 0.007, 0.017, 0.093, 0.048, 0.056, 0.046, 0.009]
46
- inclinations = [0, 7.005, 3.394, 0, 1.850, 1.308, 2.488, 0.773, 1.774]
47
 
48
- # Set up orbit traces
49
- traces_orbits = [
50
- orbits(distance_from_sun[1] * 1000000, eccentricities[1], math.radians(inclinations[1])), # Mercury
51
- orbits(distance_from_sun[2] * 1000000, eccentricities[2], math.radians(inclinations[2])), # Venus
52
- orbits(distance_from_sun[3] * 1000000, eccentricities[3], math.radians(inclinations[3])), # Earth
53
- orbits(distance_from_sun[4] * 1000000, eccentricities[4], math.radians(inclinations[4])), # Mars
54
- orbits(distance_from_sun[5] * 1000000, eccentricities[5], math.radians(inclinations[5])), # Jupiter
55
- orbits(distance_from_sun[6] * 1000000, eccentricities[6], math.radians(inclinations[6])), # Saturn
56
- orbits(distance_from_sun[7] * 1000000, eccentricities[7], math.radians(inclinations[7])), # Uranus
57
- orbits(distance_from_sun[8] * 1000000, eccentricities[8], math.radians(inclinations[8])) # Neptune
58
- ]
59
 
60
- # Use the same to draw a few rings for Saturn
61
- traces_rings_saturn = [
62
- orbits(23 * 120536 / 142984, 0, math.radians(2.488)),
63
- orbits(24 * 120536 / 142984, 0, math.radians(2.488)),
64
- orbits(25 * 120536 / 142984, 0, math.radians(2.488)),
65
- orbits(26 * 120536 / 142984, 0, math.radians(2.488)),
66
- orbits(27 * 120536 / 142984, 0, math.radians(2.488)),
67
- orbits(28 * 120536 / 142984, 0, math.radians(2.488))
68
- ]
69
 
70
- layout = go.Layout(
71
- title='Solar System',
72
- showlegend=False,
73
- margin=dict(l=0, r=0, t=0, b=0),
74
- scene=dict(
75
- xaxis=dict(title='Distance from the Sun (km)',
76
- titlefont_color='white',
77
- range=[-7e9, 7e9],
78
- backgroundcolor='black',
79
- color='white',
80
- gridcolor='gray'),
81
- yaxis=dict(title='Distance from the Sun (km)',
82
- titlefont_color='white',
83
- range=[-7e9, 7e9],
84
- backgroundcolor='black',
85
- color='white',
86
- gridcolor='gray'),
87
- zaxis=dict(title='',
88
- range=[-7e9, 7e9],
89
- backgroundcolor='black',
90
- color='white',
91
- gridcolor='gray'),
92
- annotations=[
93
- annot(0, 40, 'Sun', xancr='left'),
94
- annot(distance_from_sun[1] * 1000000, 5, 'Mercury'),
95
- annot(distance_from_sun[2] * 1000000, 9, 'Venus'),
96
- annot(distance_from_sun[3] * 1000000, 9, 'Earth'),
97
- annot(distance_from_sun[4] * 1000000, 7, 'Mars'),
98
- annot(distance_from_sun[5] * 1000000, 30, 'Jupiter'),
99
- annot(distance_from_sun[6] * 1000000, 28, 'Saturn'),
100
- annot(distance_from_sun[7] * 1000000, 20, 'Uranus'),
101
- annot(distance_from_sun[8] * 1000000, 20, 'Neptune'),
102
- ]
103
- )
104
- )
 
 
105
 
106
- fig = go.Figure(layout=layout)
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- # Add spheres for the Sun and planets
109
- fig.add_trace(go.Scatter3d(x=[0], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[0] / 2, color='#ffff00'))) # Sun
110
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[1] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[1] / 2, color='#87877d'))) # Mercury
111
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[2] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[2] / 2, color='#d23100'))) # Venus
112
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[3] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[3] / 2, color='#325bff'))) # Earth
113
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[4] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[4] / 2, color='#b20000'))) # Mars
114
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[5] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[5] / 2, color='#ebebd2'))) # Jupiter
115
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[6] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[6] / 2, color='#ebcd82'))) # Saturn
116
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[7] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[7] / 2, color='#37ffda'))) # Uranus
117
- fig.add_trace(go.Scatter3d(x=[distance_from_sun[8] * 1000000], y=[0], z=[0], mode='markers', marker=dict(size=diameter_km[8] / 2, color='#2500ab'))) # Neptune
118
 
119
- # Add orbit traces
120
- for trace in traces_orbits:
121
- trace.line.color = '#808080'
122
- fig.add_trace(trace)
123
 
124
- # Add rings for Saturn
125
- for trace in traces_rings_saturn:
126
- trace.line.color = '#827962'
127
- fig.add_trace(trace)
128
-
129
- st.plotly_chart(fig)
130
 
131
  if __name__ == "__main__":
132
- main()
 
3
  import plotly.graph_objects as go
4
  import streamlit as st
5
 
6
+ # THIS FUNCTION CREATES SHPERES
7
  def spheres(size, clr, dist=0):
8
+ # Set up 100 points. First, do angles
9
+ theta = np.linspace(0,2*np.pi,100)
10
+ phi = np.linspace(0,np.pi,100)
11
+ # Set up coordinates for points on the sphere
12
+ x0 = dist + size * np.outer(np.cos(theta),np.sin(phi))
13
+ y0 = size * np.outer(np.sin(theta),np.sin(phi))
14
+ z0 = size * np.outer(np.ones(100),np.cos(phi))
15
+ # Set up trace
16
+ trace= go.Surface(x=x0, y=y0, z=z0, colorscale=[[0,clr], [1,clr]])
17
  trace.update(showscale=False)
18
  return trace
19
 
20
+ # THIS FUNCTION CREATES ORBITS
21
  def orbits(dist, offset=0, clr='white', wdth=2):
22
+ # Initialize empty lists for eac set of coordinates
23
+ xcrd=[]
24
+ ycrd=[]
25
+ zcrd=[]
26
+ # Calculate coordinates
27
+ for i in range(0,361):
28
+ xcrd=xcrd+[(round(np.cos(math.radians(i)),5)) * dist + offset]
29
+ ycrd=ycrd+[(round(np.sin(math.radians(i)),5)) * dist]
30
+ zcrd=zcrd+[0]
31
+ trace = go.Scatter3d(x=xcrd, y=ycrd, z=zcrd, marker=dict(size=0.1), line=dict(color=clr,width=wdth))
32
  return trace
33
 
34
+ # THIS FUNCTION DEFINES ANNOTATIONS
35
  def annot(xcrd, zcrd, txt, xancr='center'):
36
+ strng=dict(showarrow=False, x=xcrd,
37
+ y=0, z=zcrd, text=txt,
38
+ xanchor=xancr, font=dict(color='#BEBEBE',size=12), opacity=0.5)
39
  return strng
40
 
41
  # The Streamlit app
42
  def main():
43
+ st.title('Solar System Visualization')
44
+ st.write('##### Zoom in to realize how small and insignificant you are!')
45
 
46
+ # VISUALIZE THE SOLAR SYSTEM
47
+ diameter_km = [1391016, 4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528]
48
+ diameter = [((i / 142984) * 20) for i in diameter_km] # Scale diameters
49
+ distance_from_sun = [0, 57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1]
 
 
 
 
50
 
51
+ # Set up orbit traces
52
+ traces_orbits = [
53
+ orbits(distance_from_sun[1]), # Mercury
54
+ orbits(distance_from_sun[2]), # Venus
55
+ orbits(distance_from_sun[3]), # Earth
56
+ orbits(distance_from_sun[4]), # Mars
57
+ orbits(distance_from_sun[5]), # Jupiter
58
+ orbits(distance_from_sun[6]), # Saturn
59
+ orbits(distance_from_sun[7]), # Uranus
60
+ orbits(distance_from_sun[8]) # Neptune
61
+ ]
62
 
63
+ # Use the same to draw a few rings for Saturn
64
+ traces_rings_saturn = [
65
+ orbits(60, distance_from_sun[6], '#827962', 3),
66
+ orbits(70, distance_from_sun[6], '#827962', 3),
67
+ orbits(80, distance_from_sun[6], '#827962', 3),
68
+ orbits(90, distance_from_sun[6], '#827962', 3),
69
+ orbits(100, distance_from_sun[6], '#827962', 3),
70
+ orbits(110, distance_from_sun[6], '#827962', 3)
71
+ ]
72
 
73
+ layout=go.Layout(
74
+ title = 'Solar System',
75
+ showlegend=False,
76
+ margin=dict(l=0, r=0, t=0, b=0),
77
+ scene = dict(
78
+ xaxis=dict(title='Distance from the Sun',
79
+ titlefont_color='black',
80
+ range=[-7000,7000],
81
+ backgroundcolor='black',
82
+ color='black',
83
+ gridcolor='black'),
84
+ yaxis=dict(title='Distance from the Sun',
85
+ titlefont_color='black',
86
+ range=[-7000,7000],
87
+ backgroundcolor='black',
88
+ color='black',
89
+ gridcolor='black'
90
+ ),
91
+ zaxis=dict(title='',
92
+ range=[-7000,7000],
93
+ backgroundcolor='black',
94
+ color='white',
95
+ gridcolor='black'
96
+ ),
97
+ annotations=[
98
+ annot(distance_from_sun[0], 200, 'Sun', xancr='left'),
99
+ annot(distance_from_sun[1], 2, 'Mercury'),
100
+ annot(distance_from_sun[2], 6, 'Venus'),
101
+ annot(distance_from_sun[3], 6, 'Earth'),
102
+ annot(distance_from_sun[4], 3, 'Mars'),
103
+ annot(distance_from_sun[5], 70, 'Jupiter'),
104
+ annot(distance_from_sun[6], 60, 'Saturn'),
105
+ annot(distance_from_sun[7], 25, 'Uranus'),
106
+ annot(distance_from_sun[8], 25, 'Neptune'),
107
+ ]
108
+ )
109
+ )
110
 
111
+ fig = go.Figure(layout=layout)
112
+
113
+ # Add spheres for the Sun and planets
114
+ fig.add_trace(spheres(diameter[0], '#ffff00', distance_from_sun[0])) # Sun
115
+ fig.add_trace(spheres(diameter[1], '#87877d', distance_from_sun[1])) # Mercury
116
+ fig.add_trace(spheres(diameter[2], '#d23100', distance_from_sun[2])) # Venus
117
+ fig.add_trace(spheres(diameter[3], '#325bff', distance_from_sun[3])) # Earth
118
+ fig.add_trace(spheres(diameter[4], '#b20000', distance_from_sun[4])) # Mars
119
+ fig.add_trace(spheres(diameter[5], '#ebebd2', distance_from_sun[5])) # Jupiter
120
+ fig.add_trace(spheres(diameter[6], '#ebcd82', distance_from_sun[6])) # Saturn
121
+ fig.add_trace(spheres(diameter[7], '#37ffda', distance_from_sun[7])) # Uranus
122
+ fig.add_trace(spheres(diameter[8], '#2500ab', distance_from_sun[8])) # Neptune
123
 
124
+ # Add orbit traces
125
+ for trace in traces_orbits:
126
+ trace.line.color = '#808080'
127
+ fig.add_trace(trace)
 
 
 
 
 
 
128
 
129
+ # Add rings for Saturn
130
+ for trace in traces_rings_saturn:
131
+ fig.add_trace(trace)
 
132
 
133
+ st.plotly_chart(fig)
 
 
 
 
 
134
 
135
  if __name__ == "__main__":
136
+ main()