ibrahim yıldız commited on
Commit
c18dbd4
·
verified ·
1 Parent(s): 00fe021

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -100
app.py CHANGED
@@ -3,135 +3,79 @@ import math
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
-
42
  # The Streamlit app
43
  def main():
44
  st.title('Solar System Visualization')
45
  st.write('##### Zoom in to realize how small and insignificant you are!')
46
 
47
- # VISUALIZE THE SOLAR SYSTEM
48
- diameter_km = [200000, 4878, 12104, 12756, 6787, 142796, 120660, 51118, 48600]
49
- diameter = [((i / 12756) * 2) for i in diameter_km]
50
- distance_from_sun = [0, 57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1]
51
-
52
- # Set up orbit traces
53
- traces_orbits = [
54
- orbits(distance_from_sun[1]), # Mercury
55
- orbits(distance_from_sun[2]), # Venus
56
- orbits(distance_from_sun[3]), # Earth
57
- orbits(distance_from_sun[4]), # Mars
58
- orbits(distance_from_sun[5]), # Jupiter
59
- orbits(distance_from_sun[6]), # Saturn
60
- orbits(distance_from_sun[7]), # Uranus
61
- orbits(distance_from_sun[8]) # Neptune
62
- ]
63
 
64
- # Use the same to draw a few rings for Saturn
65
- traces_rings_saturn = [
66
- orbits(23, distance_from_sun[6], '#827962', 3),
67
- orbits(24, distance_from_sun[6], '#827962', 3),
68
- orbits(25, distance_from_sun[6], '#827962', 3),
69
- orbits(26, distance_from_sun[6], '#827962', 3),
70
- orbits(27, distance_from_sun[6], '#827962', 3),
71
- orbits(28, distance_from_sun[6], '#827962', 3)
72
- ]
73
 
74
- layout=go.Layout(
75
- title = 'Solar System',
76
  showlegend=False,
77
  margin=dict(l=0, r=0, t=0, b=0),
78
- scene = dict(
79
- xaxis=dict(title='Distance from the Sun',
80
- titlefont_color='black',
81
- range=[-7000,7000],
82
- backgroundcolor='black',
83
- color='black',
84
- gridcolor='black'),
85
- yaxis=dict(title='Distance from the Sun',
86
- titlefont_color='black',
87
- range=[-7000,7000],
88
- backgroundcolor='black',
89
- color='black',
90
- gridcolor='black'
91
- ),
92
- zaxis=dict(title='',
93
- range=[-7000,7000],
94
- backgroundcolor='black',
95
- color='white',
96
- gridcolor='black'
97
- ),
98
  annotations=[
99
- annot(distance_from_sun[0], 40, 'Sun', xancr='left'),
100
- annot(distance_from_sun[1], 5, 'Mercury'),
101
- annot(distance_from_sun[2], 9, 'Venus'),
102
- annot(distance_from_sun[3], 9, 'Earth'),
103
- annot(distance_from_sun[4], 7, 'Mars'),
104
- annot(distance_from_sun[5], 30, 'Jupiter'),
105
- annot(distance_from_sun[6], 28, 'Saturn'),
106
- annot(distance_from_sun[7], 20, 'Uranus'),
107
- annot(distance_from_sun[8], 20, 'Neptune'),
108
  ]
109
  )
110
  )
111
 
112
  fig = go.Figure(layout=layout)
113
-
114
  # Add spheres for the Sun and planets
115
- fig.add_trace(spheres(diameter[0], '#ffff00', distance_from_sun[0])) # Sun
116
- fig.add_trace(spheres(diameter[1], '#87877d', distance_from_sun[1])) # Mercury
117
- fig.add_trace(spheres(diameter[2], '#d23100', distance_from_sun[2])) # Venus
118
- fig.add_trace(spheres(diameter[3], '#325bff', distance_from_sun[3])) # Earth
119
- fig.add_trace(spheres(diameter[4], '#b20000', distance_from_sun[4])) # Mars
120
- fig.add_trace(spheres(diameter[5], '#ebebd2', distance_from_sun[5])) # Jupiter
121
- fig.add_trace(spheres(diameter[6], '#ebcd82', distance_from_sun[6])) # Saturn
122
- fig.add_trace(spheres(diameter[7], '#37ffda', distance_from_sun[7])) # Uranus
123
- fig.add_trace(spheres(diameter[8], '#2500ab', distance_from_sun[8])) # Neptune
124
 
125
  # Add orbit traces
126
- for trace in traces_orbits:
127
- trace.line.color = '#808080'
128
- fig.add_trace(trace)
129
-
130
- # Add rings for Saturn
131
- for trace in traces_rings_saturn:
132
- fig.add_trace(trace)
133
 
134
  st.plotly_chart(fig)
135
 
136
  if __name__ == "__main__":
137
- main()
 
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
+ # Data for the sizes and distances of the planets (in km)
40
+ diameter_km = [1392684, 12742, 6780, 142984, 120536, 51118, 49528, 2370]
41
+ distance_from_sun_km = [0, 57909175, 108208930, 149597890, 227936640, 778412020, 1433449370, 2870671400]
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # Scale factors for visualization
44
+ scale_factor_diameter = 1e-5
45
+ scale_factor_distance = 1e-6
 
 
 
 
 
 
46
 
47
+ layout = go.Layout(
48
+ title='Solar System',
49
  showlegend=False,
50
  margin=dict(l=0, r=0, t=0, b=0),
51
+ scene=dict(
52
+ xaxis=dict(title='Distance from the Sun (million km)', titlefont_color='black', range=[-500, 500]),
53
+ yaxis=dict(title='Distance from the Sun (million km)', titlefont_color='black', range=[-500, 500]),
54
+ zaxis=dict(title='', range=[-500, 500]),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  annotations=[
56
+ annot(distance_from_sun_km[0] * scale_factor_distance, 40, 'Sun', xancr='left'),
57
+ annot(distance_from_sun_km[1] * scale_factor_distance, 5, 'Mercury'),
58
+ annot(distance_from_sun_km[2] * scale_factor_distance, 9, 'Venus'),
59
+ annot(distance_from_sun_km[3] * scale_factor_distance, 9, 'Earth'),
60
+ annot(distance_from_sun_km[4] * scale_factor_distance, 7, 'Mars'),
61
+ annot(distance_from_sun_km[5] * scale_factor_distance, 30, 'Jupiter'),
62
+ annot(distance_from_sun_km[6] * scale_factor_distance, 28, 'Saturn'),
63
+ annot(distance_from_sun_km[7] * scale_factor_distance, 20, 'Uranus'),
 
64
  ]
65
  )
66
  )
67
 
68
  fig = go.Figure(layout=layout)
69
+
70
  # Add spheres for the Sun and planets
71
+ for i in range(len(diameter_km)):
72
+ fig.add_trace(spheres(diameter_km[i] * scale_factor_diameter, '#ffff00', distance_from_sun_km[i] * scale_factor_distance))
 
 
 
 
 
 
 
73
 
74
  # Add orbit traces
75
+ for i in range(1, len(distance_from_sun_km)):
76
+ fig.add_trace(orbits(distance_from_sun_km[i] * scale_factor_distance, clr='#BEBEBE', wdth=1))
 
 
 
 
 
77
 
78
  st.plotly_chart(fig)
79
 
80
  if __name__ == "__main__":
81
+ main()