Debian Codegraph @

examples/02-plot/themes.py

raw
1 """
2 .. _themes_example:
3
4 Control Global and Local Plotting Themes
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 PyVista allows you to set global and local plotting themes to easily
8 set default plotting parameters. This example shows how to use the
9 :ref:`theme_api` and :func:`~pyvista.set_plot_theme` function.
10
11 """
12
13 import pyvista as pv
14 from pyvista import examples
15
16 # %%
17 # Define a simple plotting routine for comparing the themes.
18
19 mesh = examples.download_st_helens().warp_by_scalar()
20
21
22 def plot_example():
23 pl = pv.Plotter()
24 pl.add_mesh(mesh)
25 pl.add_bounding_box()
26 pl.show()
27
28
29 # %%
30 # PyVista's default color theme is chosen to be generally easy on your
31 # eyes and is best used when working long hours on your visualization
32 # project. The grey background and warm colormaps are chosen to make
33 # sure 3D renderings do not drastically change the brightness of your
34 # screen when working in dark environments.
35 #
36 # Here's an example of our default plotting theme - this is what you
37 # would see by default after running any of our examples locally.
38
39 pv.set_plot_theme('default')
40 plot_example()
41
42 # %%
43 # PyVista also ships with a few plotting themes:
44 #
45 # * ``'ParaView'``: this is designed to mimic ParaView's default plotting theme.
46 # * ``'dark'``: this is designed to be night-mode friendly with dark backgrounds and
47 # color schemes.
48 # * ``'document'``: this is built for use in document style plotting and making
49 # publication quality figures.
50
51 # %%
52 # Demo the ``'ParaView'`` theme.
53
54 pv.set_plot_theme('paraview')
55
56 plot_example()
57
58
59 # %%
60 # Demo the ``'dark'`` theme.
61
62 pv.set_plot_theme('dark')
63
64 plot_example()
65
66 # %%
67 # Demo the ``'document'`` theme. This theme is used on our online examples.
68
69 pv.set_plot_theme('document')
70
71 plot_example()
72
73 # %%
74 # Note that you can also use color gradients for the background of the plotting
75 # window.
76 pl = pv.Plotter()
77 pl.add_mesh(mesh)
78 pl.show_grid()
79 # Here we set the gradient
80 pl.set_background('royalblue', top='aliceblue')
81 cpos = pl.show()
82
83
84 # %%
85 # Modifying the Global Theme
86 # ~~~~~~~~~~~~~~~~~~~~~~~~~~
87 # You can control how meshes are displayed by setting individual
88 # parameters when plotting like ``mesh.plot(show_edges=True)``, or by
89 # setting a global theme. You can also control individual parameters
90 # how all meshes are displayed by default via ``pyvista.global_theme``.
91 #
92 # Here, we print out the current global defaults for all ``pyvista``
93 # meshes. These values have been changed by the previous "Document"
94 # theme.
95
96 pv.global_theme
97
98
99 # %%
100 # By default, edges are not shown on meshes unless explicitly
101 # specified when plotting a mesh via ``show_edges=True``. You can
102 # change this default behavior globally by changing the default
103 # parameter.
104
105 pv.global_theme.show_edges = True
106 cpos = pv.Sphere().plot()
107
108
109 # %%
110 # You can reset pyvista to default behavior with ``restore_defaults``.
111 # Note that the figure's color was reset to the default "white" color
112 # rather than the 'lightblue' color default with the document theme. Under
113 # the hood, each theme applied changes the global plot defaults stored
114 # within ``pyvista.global_theme.``
115
116 pv.global_theme.restore_defaults()
117 cpos = pv.Sphere().plot()
118
119
120 # %%
121 # Creating a Custom Theme and Applying it Globally
122 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123 # You can create a custom theme by modifying one of the existing
124 # themes and then loading it into the global plotting defaults.
125 #
126 # Here, we create a dark theme that plots meshes red by default while
127 # showing edges.
128
129 from pyvista import themes
130
131 my_theme = themes.DarkTheme()
132 my_theme.color = 'red'
133 my_theme.lighting = False
134 my_theme.show_edges = True
135 my_theme.axes.box = True
136
137 pv.global_theme.load_theme(my_theme)
138 cpos = pv.Sphere().plot()
139
140
141 # %%
142 # Creating a Custom Theme and Applying it to a Single Plotter
143 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144 # In this example, we create a custom theme from the base "default"
145 # theme and then apply it to a single plotter. Note that this does
146 # not change the behavior of the global "defaults", which are still
147 # set to the modified ``DarkTheme``.
148 #
149 # This approach carries the advantage that you can maintain several
150 # themes and apply them to one or more plotters.
151
152 from pyvista import themes
153
154 my_theme = themes.DocumentTheme()
155 my_theme.color = 'black'
156 my_theme.lighting = True
157 my_theme.show_edges = True
158 my_theme.edge_color = 'white'
159 my_theme.background = 'white'
160
161 cpos = pv.Sphere().plot(theme=my_theme)
162
163
164 # %%
165 # Alternatively, set the theme of an instance of ``Plotter``.
166
167 pl = pv.Plotter(theme=my_theme)
168 pl.add_mesh(pv.Cube())
169 cpos = pl.show()
170
171
172 # %%
173 # Reset to use the document theme
174 pv.set_plot_theme('document')
175 # %%
176 # .. tags:: plot