Debian Codegraph @

examples/02-plot/colormap.py

raw
1 """
2 .. _colormap_example:
3
4 Colormap Choices
5 ~~~~~~~~~~~~~~~~
6
7 Use a Matplotlib, Colorcet, cmocean, or custom colormap when plotting scalar
8 values with :func:`pyvista.plot` and :class:`~pyvista.Plotter` methods.
9 """
10
11 from matplotlib.colors import LinearSegmentedColormap
12 from matplotlib.colors import ListedColormap
13 import matplotlib.pyplot as plt
14 import numpy as np
15 import pyvista as pv
16 from pyvista import examples
17
18 # %%
19 # Any colormap built for ``matplotlib``, ``colorcet``, or ``cmocean`` is fully
20 # compatible with PyVista. Colormaps are typically specified by passing the
21 # string name of the colormap to the plotting routine via the ``cmap``
22 # argument.
23 #
24 # See `Matplotlib's complete list of available colormaps`_,
25 # `Colorcet's complete list`_, and `cmocean's complete list`_.
26 #
27 # .. _Matplotlib's complete list of available colormaps: https://matplotlib.org/tutorials/colors/colormaps.html
28 # .. _Colorcet's complete list: https://colorcet.holoviz.org/user_guide/index.html
29 # .. _cmocean's complete list: https://matplotlib.org/cmocean/
30
31 # %%
32 # Custom Made Colormaps
33 # +++++++++++++++++++++
34 #
35 # To get started using a custom colormap, download some data with scalar values to
36 # plot.
37
38 mesh = examples.download_st_helens().warp_by_scalar()
39 # Add scalar array with range (0, 100) that correlates with elevation
40 mesh['values'] = pv.plotting.tools.normalize(mesh['Elevation']) * 100
41
42 # %%
43 # Build a custom colormap - here we make a colormap with 5 discrete colors
44 # and we specify the ranges where those colors fall:
45
46 # Define the colors we want to use
47 blue = np.array([12 / 256, 238 / 256, 246 / 256, 1.0])
48 black = np.array([11 / 256, 11 / 256, 11 / 256, 1.0])
49 grey = np.array([189 / 256, 189 / 256, 189 / 256, 1.0])
50 yellow = np.array([255 / 256, 247 / 256, 0 / 256, 1.0])
51 red = np.array([1.0, 0.0, 0.0, 1.0])
52
53 mapping = np.linspace(mesh['values'].min(), mesh['values'].max(), 256)
54 newcolors = np.empty((256, 4))
55 newcolors[mapping >= 80] = red
56 newcolors[mapping < 80] = grey
57 newcolors[mapping < 55] = yellow
58 newcolors[mapping < 30] = blue
59 newcolors[mapping < 1] = black
60
61 # Make the colormap from the listed colors
62 my_colormap = ListedColormap(newcolors)
63
64 # %%
65 # Simply pass the colormap to the plotting routine.
66 mesh.plot(scalars='values', cmap=my_colormap)
67
68 # %%
69 # Or you could make a simple colormap... any Matplotlib colormap can be passed
70 # to PyVista.
71 boring_cmap = plt.get_cmap('viridis', 5)
72 mesh.plot(scalars='values', cmap=boring_cmap)
73
74 # %%
75 # You can also pass a list of color strings to the color map. This
76 # approach divides up the colormap into 5 equal parts.
77 mesh.plot(scalars=mesh['values'], cmap=['black', 'blue', 'yellow', 'grey', 'red'])
78
79 # %%
80 # If you still wish to have control of the separation of values, you
81 # can do this by creating a scalar array and passing that to the
82 # plotter along with the colormap
83 scalars = np.empty(mesh.n_points)
84 scalars[mesh['values'] >= 80] = 4 # red
85 scalars[mesh['values'] < 80] = 3 # grey
86 scalars[mesh['values'] < 55] = 2 # yellow
87 scalars[mesh['values'] < 30] = 1 # blue
88 scalars[mesh['values'] < 1] = 0 # black
89
90 mesh.plot(scalars=scalars, cmap=['black', 'blue', 'yellow', 'grey', 'red'])
91
92 # %%
93 # Matplotlib vs. Colorcet
94 # +++++++++++++++++++++++
95 #
96 # Let's compare Colorcet's perceptually uniform "fire" colormap to Matplotlib's
97 # "hot" colormap much like the example on the `first page of Colorcet's docs`_.
98 #
99 # .. _first page of Colorcet's docs: https://colorcet.holoviz.org/index.html
100 #
101 # The "hot" version washes out detail at the high end, as if the image is
102 # overexposed, while "fire" makes detail visible throughout the data range.
103 #
104 # Please note that in order to use Colorcet's colormaps including "fire", you
105 # must have Colorcet installed in your Python environment:
106 # ``pip install colorcet``
107
108 pl = pv.Plotter(shape=(2, 2), border=False)
109 pl.subplot(0, 0)
110 pl.add_mesh(
111 mesh,
112 scalars='Elevation',
113 cmap='fire',
114 lighting=True,
115 scalar_bar_args={'title': 'Colorcet Fire'},
116 )
117
118 pl.subplot(0, 1)
119 pl.add_mesh(
120 mesh,
121 scalars='Elevation',
122 cmap='fire',
123 lighting=False,
124 scalar_bar_args={'title': 'Colorcet Fire (No Lighting)'},
125 )
126
127 pl.subplot(1, 0)
128 pl.add_mesh(
129 mesh,
130 scalars='Elevation',
131 cmap='hot',
132 lighting=True,
133 scalar_bar_args={'title': 'Matplotlib Hot'},
134 )
135
136 pl.subplot(1, 1)
137 pl.add_mesh(
138 mesh,
139 scalars='Elevation',
140 cmap='hot',
141 lighting=False,
142 scalar_bar_args={'title': 'Matplotlib Hot (No Lighting)'},
143 )
144
145 pl.show()
146
147 # %%
148 # .. _calculix_colormap_example:
149 #
150 # Recreating Native Solver Colormaps (CalculiX)
151 # +++++++++++++++++++++++++++++++++++++++++++++
152 # Sometimes you want to process results in PyVista but keep the visual
153 # style identical to the solver's native post-processing tool.
154 # Below is an example of how to recreate the default ``cgx`` (CalculiX GraphiX)
155 # colormap using Matplotlib's :class:`matplotlib.colors.LinearSegmentedColormap`.
156 # It may be particular useful when visualizing ``.frd`` files with
157 # :class:`~pyvista.FRDReader`.
158 #
159 # This specific colormap uses exact RGB nodes at specific scalar fractions
160 # (0.0, 0.1, 0.3, 0.5, 0.8, 0.9, 1.0) to create its distinct color bands.
161
162 # Define the exact RGB nodes from the cgx source code
163 cgx_nodes = [
164 (0.0, (0.50, 0.0, 1.0)), # Purple
165 (0.1, (0.00, 0.0, 0.8)), # Dark Blue
166 (0.3, (0.00, 1.0, 0.4)), # Greenish Blue
167 (0.5, (0.00, 0.5, 0.0)), # Dark Green
168 (0.8, (1.00, 1.0, 0.0)), # Yellow
169 (0.9, (1.00, 0.5, 0.0)), # Orange
170 (1.0, (0.75, 0.0, 0.0)), # Dark Red
171 ]
172
173 # Create the colormap
174 cgx_cmap = LinearSegmentedColormap.from_list('cgx', cgx_nodes)
175
176 # %%
177 # Download a sample mesh to demonstrate the colormap
178 mesh = examples.download_notch_stress()
179
180 # %%
181 # Plot the mesh using the custom cgx colormap
182 cpos = pv.CameraPosition(
183 position=(0.1249, -0.1242, 0.1698),
184 focal_point=(0.1965, 0.0467, -0.0036),
185 viewup=(0.2512, 0.6356, 0.7299),
186 )
187
188 pl = pv.Plotter()
189 pl.add_mesh(
190 mesh,
191 cmap=cgx_cmap,
192 n_colors=21,
193 show_edges=True,
194 )
195 pl.camera_position = cpos
196 pl.show()
197
198 # %%
199 # .. tags:: plot