Debian Codegraph @

examples/99-advanced/planets.py

raw
1 """
2 .. _planets_example:
3
4 3D Earth and Celestial Bodies
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Plot the solar system in PyVista using datasets from the
8 :mod:`~pyvista.examples.planets` module.
9
10 This example is inspired by `planet3D-MATLAB
11 <https://github.com/tamaskis/planet3D-MATLAB>`_.
12
13 .. note::
14 The purpose of this example is to demonstrate plotting celestial bodies and
15 may lack astronomical precision. There may be inaccuracies in the
16 representation, so please take care when reusing or repurposing this
17 example.
18
19 Please take a look at libraries like `astropy <https://www.astropy.org/>`_
20 if you wish to use Python for astronomical calculations.
21
22 """
23
24 import pyvista as pv
25 from pyvista import examples
26
27 # sphinx_gallery_start_ignore
28 # background images do not work in interactive plots currently
29 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
30 # sphinx_gallery_end_ignore
31
32 # %%
33 # Plot the Solar System with Stars in the Background
34 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 # This section relies on calculations in `Visualizing Celestial Bodies in 3D
36 # <https://tamaskis.github.io/files/Visualizing_Celestial_Bodies_in_3D.pdf>`_.
37
38
39 # Light of the Sun.
40 light = pv.Light()
41 light.set_direction_angle(30, -20)
42
43 # Load planets
44 mercury = examples.planets.load_mercury(radius=2439.0)
45 mercury_texture = examples.planets.download_mercury_surface(texture=True)
46 venus = examples.planets.load_venus(radius=6052.0)
47 venus_texture = examples.planets.download_venus_surface(texture=True)
48 earth = examples.planets.load_earth(radius=6378.1)
49 earth_texture = examples.load_globe_texture()
50 mars = examples.planets.load_mars(radius=3397.2)
51 mars_texture = examples.planets.download_mars_surface(texture=True)
52 jupiter = examples.planets.load_jupiter(radius=71492.0)
53 jupiter_texture = examples.planets.download_jupiter_surface(texture=True)
54 saturn = examples.planets.load_saturn(radius=60268.0)
55 saturn_texture = examples.planets.download_saturn_surface(texture=True)
56 # Saturn's rings range from 7000.0 km to 80000.0 km from the surface of the planet
57 inner = 60268.0 + 7000.0
58 outer = 60268.0 + 80000.0
59 saturn_rings = examples.planets.load_saturn_rings(inner=inner, outer=outer, c_res=50)
60 saturn_rings_texture = examples.planets.download_saturn_rings(texture=True)
61 uranus = examples.planets.load_uranus(radius=25559.0)
62 uranus_texture = examples.planets.download_uranus_surface(texture=True)
63 neptune = examples.planets.load_neptune(radius=24764.0)
64 neptune_texture = examples.planets.download_neptune_surface(texture=True)
65 pluto = examples.planets.load_pluto(radius=1151.0)
66 pluto_texture = examples.planets.download_pluto_surface(texture=True)
67
68 # Move planets to a nice position for the plotter. These numbers are not
69 # grounded in reality and are for demonstration purposes only.
70 mercury.translate((0.0, 0.0, 0.0), inplace=True)
71 venus.translate((-15000.0, 0.0, 0.0), inplace=True)
72 earth.translate((-30000.0, 0.0, 0.0), inplace=True)
73 mars.translate((-45000.0, 0.0, 0.0), inplace=True)
74 jupiter.translate((-150000.0, 0.0, 0.0), inplace=True)
75 saturn.translate((-400000.0, 0.0, 0.0), inplace=True)
76 saturn_rings.translate((-400000.0, 0.0, 0.0), inplace=True)
77 uranus.translate((-600000.0, 0.0, 0.0), inplace=True)
78 neptune.translate((-700000.0, 0.0, 0.0), inplace=True)
79
80 # Add planets to Plotter.
81 pl = pv.Plotter(lighting='none')
82 cubemap = examples.download_cubemap_space_4k()
83 _ = pl.add_actor(cubemap.to_skybox())
84 pl.set_environment_texture(cubemap, is_srgb=True, resample=1 / 64)
85 pl.add_light(light)
86 pl.add_mesh(mercury, texture=mercury_texture, smooth_shading=True)
87 pl.add_mesh(venus, texture=venus_texture, smooth_shading=True)
88 pl.add_mesh(earth, texture=earth_texture, smooth_shading=True)
89 pl.add_mesh(mars, texture=mars_texture, smooth_shading=True)
90 pl.add_mesh(jupiter, texture=jupiter_texture, smooth_shading=True)
91 pl.add_mesh(saturn, texture=saturn_texture, smooth_shading=True)
92 pl.add_mesh(saturn_rings, texture=saturn_rings_texture, smooth_shading=True)
93 pl.add_mesh(uranus, texture=uranus_texture, smooth_shading=True)
94 pl.add_mesh(neptune, texture=neptune_texture, smooth_shading=True)
95 pl.add_mesh(pluto, texture=pluto_texture, smooth_shading=True)
96 pl.show()
97
98
99 # %%
100 # Plot the Planets and their Textures
101 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 # Each planet here is in a different subplot. The planet's textures are from
103 # `Solar Textures <https://www.solarsystemscope.com/textures/>`_.
104
105 pl = pv.Plotter(shape=(3, 2))
106 pl.subplot(0, 0)
107 pl.add_text('Mercury')
108 pl.add_mesh(examples.planets.download_mercury_surface(), rgb=True)
109 pl.subplot(0, 1)
110 pl.add_mesh(mercury, texture=mercury_texture)
111 pl.subplot(1, 0)
112 pl.add_text('Venus')
113 pl.add_mesh(examples.planets.download_venus_surface(atmosphere=True), rgb=True)
114 pl.subplot(1, 1)
115 pl.add_mesh(venus, texture=venus_texture)
116 pl.subplot(2, 0)
117 pl.add_text('Mars')
118 pl.add_mesh(examples.planets.download_mars_surface(), rgb=True)
119 pl.subplot(2, 1)
120 pl.add_mesh(mars, texture=mars_texture)
121 pl.show(cpos='xy')
122
123
124 # %%
125 # Plot the Atmosphere and Surface of Venus
126 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127 # Here we plot Venus with and without its atmosphere.
128
129 venus = examples.planets.load_venus()
130 atmosphere_texture = examples.planets.download_venus_surface(
131 atmosphere=True, texture=True
132 )
133 surface_texture = examples.planets.download_venus_surface(atmosphere=False, texture=True)
134
135 pl = pv.Plotter(shape=(1, 2))
136 pl.subplot(0, 0)
137 pl.add_text('Venus Atmosphere')
138 pl.add_mesh(venus, texture=atmosphere_texture, smooth_shading=True)
139 pl.subplot(0, 1)
140 pl.add_text('Venus Surface')
141 pl.add_mesh(venus, texture=surface_texture, smooth_shading=True)
142 pl.link_views()
143 pl.show(cpos='xy')