Debian Codegraph @

examples/02-plot/lookup_table.py

raw
1 """
2 .. _lookup_table_example:
3
4 Lookup Tables
5 ~~~~~~~~~~~~~
6 Demonstrate the usage of a lookup table within PyVista
7
8 The :class:`pyvista.LookupTable` can be used to have fine-tuned control over
9 the mapping between a :class:`pyvista.DataSet`'s scalars and RGBA colors.
10
11 """
12
13 import pyvista as pv
14 from pyvista import examples
15
16 # sphinx_gallery_start_ignore
17 # big figures of colormaps and widgets show better in static images
18 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
19 # sphinx_gallery_end_ignore
20
21 # download an example dataset
22 bracket = examples.download_fea_bracket().cell_data_to_point_data()
23 bracket
24
25
26 # %%
27 # Default Color Map - Lookup Table
28 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 # First, let's plot using the default color map, "viridis". Internally, PyVista
30 # will automatically create a lookup table to map the scalars (stored here
31 # within `point_data`) to RGBA colors. This is shown here as a nested attribute
32 # to the :class:`pyvista.DataSetMapper` and it has a helpful ``repr`` method:
33
34 pl = pv.Plotter()
35 actor = pl.add_mesh(bracket)
36 actor.mapper.lookup_table
37
38
39 # %%
40 # Plot the Lookup Table
41 # ~~~~~~~~~~~~~~~~~~~~~
42 # You can also plot lookup table to see the mapping between the scalar values
43 # (here, between 0 and 23.3) and RGBA colors.
44
45 pl = pv.Plotter()
46 actor = pl.add_mesh(bracket)
47 actor.mapper.lookup_table.plot()
48
49
50 # %%
51 # Plot the DataSet
52 # ~~~~~~~~~~~~~~~~
53 # Let's plot the dataset using the automatically generated lookup table.
54
55 pl = pv.Plotter()
56 pl.add_mesh(bracket)
57 pl.show()
58
59
60 # %%
61 # Create a Custom Lookup Table using a Matplotlib Color Map
62 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 # Here we create a lookup table with a narrow table range (same as ``clim``)
64 # and color values above and below the range.
65
66 lut = pv.LookupTable(cmap='magma')
67 lut.scalar_range = (5, 15)
68 lut.below_range_color = pv.Color('grey', opacity=0.5)
69 lut.above_range_color = 'r'
70 lut.plot()
71
72
73 # %%
74 # Plot the bracket with the custom colormap
75 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 # You can set assign the lookup table when using ``add_mesh`` with ``cmap=``.
77
78
79 pl = pv.Plotter()
80 actor = pl.add_mesh(bracket, cmap=lut, lighting=False)
81 pl.show()
82
83
84 # %%
85 # Create a Custom Lookup Table using VTK's Methods
86 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 # If you want to create a completely unique color map, you can use attributes
88 # like :attr:`pyvista.LookupTable.hue_range` and
89 # :attr:`pyvista.LookupTable.value_range` to create your own lookup table.
90
91 lut = pv.LookupTable()
92 lut.value_range = (0.35, 1) # dark grey to white
93 lut.hue_range = (0.35, 0.7) # green to cyna
94 lut.saturation_range = (0.75, 0.5) # reduce saturation near the upper end
95 lut.alpha_range = (0.0, 0.9)
96 lut.scalar_range = (2, 18)
97 lut.plot()
98
99
100 # %%
101 # Plot the bracket with the custom colormap
102 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103 # Assign this custom color map to the plotter and disable lighting to improve
104 # the plot.
105
106 pl = pv.Plotter()
107 actor = pl.add_mesh(bracket, cmap=lut, lighting=False)
108 pl.show()
109
110
111 # %%
112 # Custom colormap with widgets
113 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114 # Here we plot the scalars and dynamically change the lookup table through
115 # widgets. We create several overlapping single slider widgets to simulate a
116 # double ended slider widget.
117 #
118 # This example just controls the alpha channel.
119
120
121 pl = pv.Plotter()
122 actor = pl.add_mesh(bracket, cmap=lut, lighting=False)
123 pl.add_text('Alpha Range Demo')
124
125
126 def set_min_alpha(min_value):
127 max_value = lut.alpha_range[1]
128 if min_value > max_value:
129 # force the movement of the maximum value
130 max_value = min_value
131 pl.widgets.slider_widgets[1].GetRepresentation().SetValue(max_value)
132 lut.alpha_range = (min_value, max_value)
133
134
135 def set_max_alpha(max_value):
136 min_value = lut.alpha_range[0]
137 if max_value < min_value:
138 # force the movement of the minimum value
139 min_value = max_value
140 pl.widgets.slider_widgets[0].GetRepresentation().SetValue(min_value)
141
142 lut.alpha_range = (min_value, max_value)
143
144
145 # create two overlapping slider bars by hiding the tube of the second
146 pl.add_slider_widget(
147 set_min_alpha,
148 (0, 1),
149 value=lut.alpha_range[0],
150 interaction_event='always',
151 title='Alpha Range',
152 tube_width=0.003,
153 )
154 pl.add_slider_widget(
155 set_max_alpha,
156 (0, 1),
157 value=lut.alpha_range[1],
158 interaction_event='always',
159 tube_width=0.0,
160 )
161
162 pl.show()
163
164
165 # %%
166 # Control Several Lookup Table Attributes
167 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168 # Demonstrate the use of several slider bar widgets with lookup table
169 # callbacks.
170
171 # Create a new lookup table with oranges
172 lut = pv.LookupTable()
173 lut.value_range = (0.3, 0.75)
174 lut.hue_range = (0.0, 0.095)
175 lut.saturation_range = (0.0, 0.67)
176 lut.alpha_range = (0.0, 1.0)
177 lut.scalar_range = (2, 18)
178
179 scalars_rng = (bracket.active_scalars.min(), bracket.active_scalars.max())
180
181
182 def make_double_slider(attr, idx):
183 """Create a double slider for a given lookup table attribute."""
184
185 def set_min(min_value):
186 max_value = getattr(lut, attr)[1]
187 if min_value > max_value:
188 # force the movement of the maximum value
189 max_value = min_value
190 pl.widgets.slider_widgets[idx * 2 + 1].GetRepresentation().SetValue(max_value)
191 setattr(lut, attr, (min_value, max_value))
192
193 if attr == 'scalar_range':
194 actor.mapper.scalar_range = getattr(lut, attr)
195
196 def set_max(max_value):
197 min_value = getattr(lut, attr)[0]
198 if max_value < min_value:
199 # force the movement of the minimum value
200 min_value = max_value
201 pl.widgets.slider_widgets[idx * 2].GetRepresentation().SetValue(min_value)
202 setattr(lut, attr, (min_value, max_value))
203
204 if attr == 'scalar_range':
205 actor.mapper.scalar_range = getattr(lut, attr)
206
207 rng = scalars_rng if attr == 'scalar_range' else (0, 1)
208
209 # create two overlapping slider bars by hiding the tube of the second
210 pl.add_slider_widget(
211 set_min,
212 rng,
213 value=getattr(lut, attr)[0],
214 interaction_event='always',
215 title=' '.join(attr.split('_')).capitalize(),
216 tube_width=0.003,
217 pointa=(0.6, 0.9 - 0.165 * idx),
218 pointb=(0.9, 0.9 - 0.165 * idx),
219 )
220 pl.add_slider_widget(
221 set_max,
222 rng,
223 value=getattr(lut, attr)[1],
224 interaction_event='always',
225 tube_width=0.0,
226 pointa=(0.6, 0.9 - 0.165 * idx),
227 pointb=(0.9, 0.9 - 0.165 * idx),
228 )
229
230
231 pl = pv.Plotter()
232 actor = pl.add_mesh(bracket, cmap=lut, lighting=False)
233 make_double_slider('alpha_range', 0)
234 make_double_slider('hue_range', 1)
235 make_double_slider('value_range', 2)
236 make_double_slider('saturation_range', 3)
237 make_double_slider('scalar_range', 4)
238
239 pl.camera_position = pv.CameraPosition(
240 position=(9.021, 5.477, 7.780),
241 focal_point=(-0.679, 1.349, 0.874),
242 viewup=(-0.498, -0.228, 0.836),
243 )
244 cpos = pl.show(return_cpos=True)
245 # %%
246 # .. tags:: plot