Debian Codegraph @

examples/02-plot/axes_objects.py

raw
1 """
2 .. _axes_objects_example:
3
4 Axes Objects
5 ~~~~~~~~~~~~
6
7 PyVista has many axes objects which can be used for plotting.
8 This example highlights many of these objects and shows how
9 to use them with related plotting methods.
10
11 """
12
13 # sphinx_gallery_thumbnail_number = 7
14
15 import pyvista as pv
16 from pyvista import examples
17
18 # %%
19 # Cube Axes
20 # =========
21 # Show axes bounds as a cube with :class:`~pyvista.CubeAxesActor`.
22
23 mesh = examples.download_bunny_coarse()
24
25 pl = pv.Plotter()
26 pl.add_mesh(mesh)
27 axes = pv.CubeAxesActor(camera=pl.camera)
28 axes.bounds = mesh.bounds
29 pl.add_actor(axes)
30 pl.background_color = pv.Color('paraview')
31 pl.show()
32
33 # %%
34 # Adding the axes like this can be a bit cumbersome since the camera, bounds, and color
35 # must be set manually. Instead, use :meth:`~pyvista.Plotter.show_bounds` to add a
36 # :class:`~pyvista.CubeAxesActor` with pre-configured parameters.
37
38 pl = pv.Plotter()
39 pl.add_mesh(mesh)
40 pl.show_bounds()
41 pl.show()
42
43 # %%
44 # Alternatively, use :meth:`~pyvista.Plotter.show_grid`. This also adds a
45 # :class:`~pyvista.CubeAxesActor` to the plot but with different default options.
46
47 pl = pv.Plotter()
48 pl.add_mesh(mesh)
49 pl.show_grid()
50 pl.show()
51
52 # %%
53 # .. seealso::
54 #
55 # :meth:`~pyvista.Plotter.remove_bounds_axes`
56 # :meth:`~pyvista.Plotter.update_bounds_axes`
57 # :ref:`bounds_example`
58
59 # %%
60 # Arrow Axes
61 # ==========
62 # Arrow-style axes include :class:`~pyvista.AxesActor`, :class:`~pyvista.AxesAssembly`,
63 # and :class:`~pyvista.AxesAssemblySymmetric`.
64 #
65 # ``AxesActor`` is primarily intended for use as an orientation widget
66 # (see next section), but can also be added to a plot as a normal actor.
67 # Use :meth:`~pyvista.Plotter.add_axes_at_origin` to add ``AxesActor``
68 # at the origin.
69
70 pl = pv.Plotter()
71 pl.add_mesh(mesh)
72 axes = pl.add_axes_at_origin()
73 pl.show()
74
75 # %%
76 # The axes are too large and should be scaled down. Transformations with `AxesActor`
77 # are possible, but with some caveats:
78 #
79 # - The bounds of ``AxesActor`` are hard-coded as ``+/- 1``, which makes it challenging
80 # to configure the camera bounds for the plot.
81 # - The user matrix must be used for transformations (scale and position properties
82 # do not work).
83 #
84 # Create new axes, disable its bounds, and apply a scaling :class:`~pyvista.Transform`.
85
86 trans = pv.Transform().scale(0.25)
87 axes = pv.AxesActor()
88 axes.UseBoundsOff()
89 axes.SetUserMatrix(pv.vtkmatrix_from_array(trans.matrix))
90
91 # %%
92 # Plot the axes with a mesh. Note that since the bounds of the axes are not used,
93 # the tip of the z-axis appears clipped, which is not ideal.
94
95 pl = pv.Plotter()
96 pl.add_mesh(mesh)
97 pl.add_actor(axes)
98 pl.show()
99
100 # %%
101 # Instead of using :class:`~pyvista.AxesActor`, :class:`~pyvista.AxesAssembly` is
102 # recommended for positioning axes in a scene.
103
104 axes = pv.AxesAssembly(scale=0.25)
105 pl = pv.Plotter()
106 pl.add_mesh(mesh)
107 pl.add_actor(axes)
108 pl.show()
109
110 # %%
111 # Alternatively, use :class:`~pyvista.AxesAssemblySymmetric` for adding
112 # symmetric axes to a scene.
113
114 axes = pv.AxesAssemblySymmetric(scale=0.25)
115 pl = pv.Plotter()
116 pl.add_mesh(mesh)
117 pl.add_actor(axes)
118 pl.show()
119
120 # %%
121 # Axes Widgets
122 # ============
123 # Any actor can also be used as an axes orientation widget.
124 # Here, we demonstrate using four separate axes widgets:
125 #
126 # #. Use :meth:`~pyvista.Plotter.add_axes` to add an arrow-style
127 # orientation widget. The widget uses :class:`~pyvista.AxesActor`
128 # by default.
129 # #. Use :meth:`~pyvista.Plotter.add_box_axes` to add a box-style
130 # orientation widget.
131 # #. Use :meth:`~pyvista.Plotter.add_north_arrow_widget` to add a
132 # north arrow orientation widget.
133 # #. Add :class:`~pyvista.AxesAssemblySymmetric` as a custom
134 # orientation widget using :meth:`~pyvista.Plotter.add_orientation_widget`.
135
136 # Load a dataset
137 mesh = examples.load_airplane()
138
139 # Create a plotter with four linked views.
140 viewport = (0, 0, 0.5, 0.5)
141 pl = pv.Plotter(shape=(2, 2))
142 pl.link_views()
143
144 # Add arrow-style axes
145 pl.subplot(0, 0)
146 pl.add_mesh(mesh)
147 pl.add_axes(viewport=viewport)
148
149 # Add box-style axes
150 pl.subplot(0, 1)
151 pl.add_mesh(mesh)
152 pl.add_box_axes(viewport=viewport)
153
154 # Add north arrow
155 pl.subplot(1, 0)
156 pl.add_mesh(mesh)
157 pl.add_north_arrow_widget(viewport=viewport)
158
159 # Add symmetric arrow-style axes
160 pl.subplot(1, 1)
161 pl.add_mesh(mesh)
162 axes = pv.AxesAssemblySymmetric(label_size=25)
163 pl.add_orientation_widget(axes, viewport=viewport)
164
165 pl.show()
166
167 # %%
168 # Camera Orientation Widget
169 # -------------------------
170 # There is also a specialized camera widget which can added to a plot with
171 # :class:`~pyvista.Plotter.add_camera_orientation_widget`.
172
173 pl = pv.Plotter()
174 pl.add_mesh(mesh)
175 pl.add_camera_orientation_widget()
176 pl.show()
177
178 # %%
179 # .. tags:: plot