Debian Codegraph @

examples/02-plot/opacity.py

raw
1 """
2 .. _opacity_example:
3
4 Plot with Opacity
5 ~~~~~~~~~~~~~~~~~
6
7 Plot a mesh's scalar array with an :class:`~pyvista.opacity_transfer_function`
8 or opacity mapping based on a scalar array.
9 """
10
11 # sphinx_gallery_thumbnail_number = 2
12 import pyvista as pv
13 from pyvista import examples
14
15 # Load St Helens DEM and warp the topography
16 image = examples.download_st_helens()
17 mesh = image.warp_by_scalar()
18
19
20 # %%
21 # Global Value
22 # ++++++++++++
23 #
24 # You can also apply a global opacity value to the mesh by passing a single
25 # float between 0 and 1 which would enable you to see objects behind the mesh:
26
27 pl = pv.Plotter()
28 pl.add_mesh(
29 image.contour(),
30 line_width=5,
31 )
32 pl.add_mesh(mesh, opacity=0.85, color=True)
33 pl.show()
34
35 # %%
36 # Note that you can specify ``use_transparency=True`` to convert opacities to
37 # transparencies in any of the following examples.
38
39
40 # %%
41 # Transfer Functions
42 # ++++++++++++++++++
43 #
44 # It's possible to apply an opacity mapping to any scalar array plotted. You
45 # can specify either a single static value to make the mesh transparent on all
46 # cells, or use a transfer function where the scalar array plotted is mapped
47 # to the opacity. We have several predefined transfer functions.
48 #
49 # Opacity transfer functions are:
50 #
51 # - ``'linear'``: linearly vary (increase) opacity across the plotted scalar
52 # range from low to high
53 # - ``'linear_r'``: linearly vary (increase) opacity across the plotted scalar
54 # range from high to low
55 # - ``'geom'``: on a log scale, vary (increase) opacity across the plotted scalar
56 # range from low to high
57 # - ``'geom_r'``: on a log scale, vary (increase) opacity across the plotted scalar
58 # range from high to low
59 # - ``'sigmoid'``: vary (increase) opacity on a sigmoidal s-curve across the plotted
60 # scalar range from low to high
61 # - ``'sigmoid_r'``: vary (increase) opacity on a sigmoidal s-curve across the plotted
62 # scalar range from high to low
63
64 # Show the linear opacity transfer function
65 mesh.plot(opacity='linear')
66
67 # %%
68
69 # Show the sigmoid opacity transfer function
70 mesh.plot(opacity='sigmoid')
71
72 # %%
73 # It's also possible to use your own transfer function that will be linearly
74 # mapped to the scalar array plotted. For example, we can create an opacity
75 # mapping as:
76 opacity = [0, 0.2, 0.9, 0.6, 0.3]
77
78 # %%
79 # When given a minimized opacity mapping like that above, PyVista interpolates
80 # it across a range of how many colors are shown when mapping the scalars.
81 # Linear interpolation is used by default, but other kinds of interpolation
82 # can be used if ``scipy`` is available.
83 # Curious what that opacity transfer function looks like? You can fetch it:
84
85 # Have PyVista interpolate the transfer function
86 tf = pv.opacity_transfer_function(opacity, 256).astype(float) / 255.0
87
88 import matplotlib.pyplot as plt
89
90 plt.plot(tf)
91 plt.title('My Interpolated Opacity Transfer Function')
92 plt.ylabel('Opacity')
93 plt.xlabel('Index along scalar mapping')
94 plt.show()
95
96 # %%
97 # That opacity mapping will have an opacity of 0.0 at the minimum scalar range,
98 # a value or 0.9 at the middle of the scalar range, and a value of 0.3 at the
99 # maximum of the scalar range:
100
101 mesh.plot(opacity=opacity)
102
103 # %%
104 # Opacity mapping is often useful when plotting DICOM images. For example,
105 # download the sample knee DICOM image:
106 knee = examples.download_knee()
107
108 # %%
109 # And here we inspect the DICOM image with a few different opacity mappings:
110 pl = pv.Plotter(shape=(2, 2), border=False)
111
112 pl.add_mesh(knee, cmap='bone', scalar_bar_args={'title': 'No Opacity'})
113 pl.view_xy()
114
115 pl.subplot(0, 1)
116 pl.add_mesh(
117 knee, cmap='bone', opacity='linear', scalar_bar_args={'title': 'Linear Opacity'}
118 )
119 pl.view_xy()
120
121 pl.subplot(1, 0)
122 pl.add_mesh(
123 knee, cmap='bone', opacity='sigmoid', scalar_bar_args={'title': 'Sigmoidal Opacity'}
124 )
125 pl.view_xy()
126
127 pl.subplot(1, 1)
128 pl.add_mesh(
129 knee, cmap='bone', opacity='geom_r', scalar_bar_args={'title': 'Log Scale Opacity'}
130 )
131 pl.view_xy()
132
133
134 pl.show()
135
136 # %%
137 # Opacity by Array
138 # ++++++++++++++++
139 #
140 # You can also use a scalar array associated with the mesh to give each cell
141 # its own opacity/transparency value derived from a scalar field. For example,
142 # an uncertainty array from a modelling result could be used to hide regions of
143 # a mesh that are uncertain and highlight regions that are well resolved.
144 #
145 # The following is a demonstration of plotting a mesh with colored values and
146 # using a second array to control the transparency of the mesh
147
148 model = examples.download_model_with_variance()
149 contours = model.contour(10, scalars='Temperature')
150 contours.array_names
151
152 # %%
153 # Make sure to flag ``use_transparency=True`` since we want areas of high
154 # variance to have high transparency.
155 #
156 # Also, since the opacity array must be between 0 and 1, we normalize
157 # the temperature variance array by the maximum value. That way high
158 # variance will be completely transparent.
159
160 contours['Temperature_var'] /= contours['Temperature_var'].max()
161
162 pl = pv.Plotter(shape=(1, 2))
163
164 pl.subplot(0, 0)
165 pl.add_text('Opacity by Array')
166 pl.add_mesh(
167 contours.copy(),
168 scalars='Temperature',
169 opacity='Temperature_var',
170 use_transparency=True,
171 cmap='bwr',
172 )
173
174 pl.subplot(0, 1)
175 pl.add_text('No Opacity')
176 pl.add_mesh(contours, scalars='Temperature', cmap='bwr')
177 pl.show()
178 # %%
179 # .. tags:: plot