| 1 |
""" |
| 2 |
.. _sharing_scalar_bars_example: |
| 3 |
|
| 4 |
Sharing Scalar Bars |
| 5 |
~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Use :meth:`~pyvista.Plotter.subplot` and show to share scalar bars |
| 8 |
among plotted arrays or use a unique scalar bar for each plotted array. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
# sphinx_gallery_thumbnail_number = 3 |
| 13 |
import pyvista as pv |
| 14 |
|
| 15 |
# sphinx_gallery_start_ignore |
| 16 |
# setting scalar bar position does not work in interactive plots |
| 17 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 18 |
# sphinx_gallery_end_ignore |
| 19 |
|
| 20 |
# %% |
| 21 |
# By default, a single scalar bar is used for all plotted scalars with the same name: |
| 22 |
|
| 23 |
# Create two cube meshes containing the scalar array scalars1 |
| 24 |
cube = pv.Cube() |
| 25 |
cube.cell_data['scalars1'] = range(0, 6, 1) |
| 26 |
second_cube = cube.copy() |
| 27 |
second_cube.cell_data['scalars1'] = range(0, 12, 2) |
| 28 |
|
| 29 |
# Plot the scalars1 array for each cube - note how there is only one scalar bar |
| 30 |
pl = pv.Plotter(shape=(1, 2), border_width=1) |
| 31 |
pl.add_mesh(cube, scalars='scalars1', cmap='coolwarm') |
| 32 |
pl.subplot(0, 1) |
| 33 |
pl.add_mesh(second_cube, scalars='scalars1', cmap='coolwarm') |
| 34 |
pl.show() |
| 35 |
|
| 36 |
# %% |
| 37 |
# To use a separate scalar bar for each plotted scalar, set the scalar bar title |
| 38 |
# to a unique string for each plot: |
| 39 |
|
| 40 |
pl = pv.Plotter(shape=(1, 2), border_width=1) |
| 41 |
pl.add_mesh( |
| 42 |
cube, |
| 43 |
scalars='scalars1', |
| 44 |
cmap='coolwarm', |
| 45 |
scalar_bar_args={'title': 'Cube 1: scalars1'}, |
| 46 |
) # Unique title |
| 47 |
pl.subplot(0, 1) |
| 48 |
pl.add_mesh( |
| 49 |
second_cube, |
| 50 |
scalars='scalars1', |
| 51 |
cmap='coolwarm', |
| 52 |
scalar_bar_args={'title': 'Cube 2: scalars1'}, |
| 53 |
) # Unique title |
| 54 |
pl.show() |
| 55 |
|
| 56 |
# %% |
| 57 |
# By default, a unique scalar bar is used for all plotted scalars with unique names: |
| 58 |
|
| 59 |
# Create a cube mesh with four scalar arrays |
| 60 |
cube = pv.Cube() |
| 61 |
cube.cell_data['scalars1'] = range(0, 6, 1) |
| 62 |
cube.cell_data['scalars2'] = range(0, 12, 2) |
| 63 |
cube.cell_data['scalars3'] = range(0, 18, 3) |
| 64 |
cube.cell_data['scalars4'] = range(0, 24, 4) |
| 65 |
|
| 66 |
# Plot the four scalar arrays - note how there is one scalar bar for each plotted |
| 67 |
# scalar. A shallow copy of cube is made when plotting each scalar array since |
| 68 |
# a mesh can have only one active scalar |
| 69 |
pl = pv.Plotter(shape=(2, 2), border_width=1) |
| 70 |
pl.add_mesh(cube, scalars='scalars1', cmap='coolwarm') |
| 71 |
pl.camera.zoom(0.75) |
| 72 |
pl.subplot(0, 1) |
| 73 |
pl.add_mesh(cube.copy(deep=False), scalars='scalars2', cmap='coolwarm') |
| 74 |
pl.camera.zoom(0.75) |
| 75 |
pl.subplot(1, 0) |
| 76 |
pl.add_mesh(cube.copy(deep=False), scalars='scalars3', cmap='coolwarm') |
| 77 |
pl.camera.zoom(0.75) |
| 78 |
pl.subplot(1, 1) |
| 79 |
pl.add_mesh(cube.copy(deep=False), scalars='scalars4', cmap='coolwarm') |
| 80 |
pl.camera.zoom(0.75) |
| 81 |
pl.show() |
| 82 |
|
| 83 |
# %% |
| 84 |
# To share a scalar bar among different subplots, use a common scalar bar title |
| 85 |
# among those subplots: |
| 86 |
|
| 87 |
# Plot the four scalar arrays - use one scalar bar for each row |
| 88 |
pl = pv.Plotter(shape=(2, 2), border_width=1) |
| 89 |
pl.add_mesh( |
| 90 |
cube, scalars='scalars1', cmap='coolwarm', scalar_bar_args={'title': 'Scalars 1 & 2'} |
| 91 |
) |
| 92 |
pl.camera.zoom(0.75) |
| 93 |
pl.subplot(0, 1) |
| 94 |
pl.add_mesh( |
| 95 |
cube.copy(deep=False), |
| 96 |
scalars='scalars2', |
| 97 |
cmap='coolwarm', |
| 98 |
scalar_bar_args={'title': 'Scalars 1 & 2'}, |
| 99 |
) |
| 100 |
pl.camera.zoom(0.75) |
| 101 |
pl.subplot(1, 0) |
| 102 |
pl.add_mesh( |
| 103 |
cube.copy(deep=False), |
| 104 |
scalars='scalars3', |
| 105 |
cmap='coolwarm', |
| 106 |
scalar_bar_args={'title': 'Scalars 3 & 4'}, |
| 107 |
) |
| 108 |
pl.camera.zoom(0.75) |
| 109 |
pl.subplot(1, 1) |
| 110 |
pl.add_mesh( |
| 111 |
cube.copy(deep=False), |
| 112 |
scalars='scalars4', |
| 113 |
cmap='coolwarm', |
| 114 |
scalar_bar_args={'title': 'Scalars 3 & 4'}, |
| 115 |
) |
| 116 |
pl.camera.zoom(0.75) |
| 117 |
pl.show() |
| 118 |
# %% |
| 119 |
# .. tags:: plot |