| 1 |
""" |
| 2 |
.. _scalar_bar_example: |
| 3 |
|
| 4 |
Customize Scalar Bars |
| 5 |
~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Walk through of all the different capabilities of scalar bars and |
| 8 |
how a user can customize scalar bars. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
# sphinx_gallery_thumbnail_number = 2 |
| 13 |
import pyvista as pv |
| 14 |
from pyvista import examples |
| 15 |
|
| 16 |
# sphinx_gallery_start_ignore |
| 17 |
# setting scalar bar position does not work in interactive plots |
| 18 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 19 |
# sphinx_gallery_end_ignore |
| 20 |
|
| 21 |
# %% |
| 22 |
# By default, when plotting a dataset with a scalar array, a scalar bar for that |
| 23 |
# array is added. To turn off this behavior, a user could specify |
| 24 |
# ``show_scalar_bar=False`` when calling :func:`~pyvista.Plotter.add_mesh`. |
| 25 |
# Let's start with a sample dataset provide via PyVista to demonstrate the default |
| 26 |
# behavior of scalar bar plotting: |
| 27 |
|
| 28 |
# Load St Helens DEM and warp the topography |
| 29 |
mesh = examples.download_st_helens().warp_by_scalar() |
| 30 |
|
| 31 |
# First a default plot with jet colormap |
| 32 |
pl = pv.Plotter() |
| 33 |
# Add the data, use active scalar for coloring, and show the scalar bar |
| 34 |
pl.add_mesh(mesh) |
| 35 |
# Display the scene |
| 36 |
pl.show() |
| 37 |
|
| 38 |
# %% |
| 39 |
# We could also plot the scene with an interactive scalar bar to move around |
| 40 |
# and place where we like by specifying passing keyword arguments to control |
| 41 |
# the scalar bar via the ``scalar_bar_args`` parameter in |
| 42 |
# :func:`pyvista.Plotter.add_mesh`. The keyword arguments to control the |
| 43 |
# scalar bar are defined in :func:`pyvista.Plotter.add_scalar_bar`. |
| 44 |
|
| 45 |
# create dictionary of parameters to control scalar bar |
| 46 |
sargs = dict(interactive=True) # Simply make the bar interactive |
| 47 |
|
| 48 |
pl = pv.Plotter(notebook=False) # If in IPython, be sure to show the scene |
| 49 |
pl.add_mesh(mesh, scalar_bar_args=sargs) |
| 50 |
pl.show() |
| 51 |
# Remove from plotters so output is not produced in docs |
| 52 |
pv.plotting.plotter._ALL_PLOTTERS.clear() |
| 53 |
|
| 54 |
|
| 55 |
# %% |
| 56 |
# .. figure:: ../../images/gifs/scalar-bar-interactive.gif |
| 57 |
# |
| 58 |
# Or manually define the scalar bar's location: |
| 59 |
|
| 60 |
# Set a custom position and size |
| 61 |
sargs = dict(height=0.25, vertical=True, position_x=0.05, position_y=0.05) |
| 62 |
|
| 63 |
pl = pv.Plotter() |
| 64 |
pl.add_mesh(mesh, scalar_bar_args=sargs) |
| 65 |
pl.show() |
| 66 |
|
| 67 |
# %% |
| 68 |
# The text properties of the scalar bar can also be controlled: |
| 69 |
|
| 70 |
# Controlling the text properties |
| 71 |
sargs = dict( |
| 72 |
title_font_size=20, |
| 73 |
label_font_size=16, |
| 74 |
shadow=True, |
| 75 |
n_labels=3, |
| 76 |
italic=True, |
| 77 |
fmt='{0:.1f}', |
| 78 |
font_family='arial', |
| 79 |
) |
| 80 |
|
| 81 |
pl = pv.Plotter() |
| 82 |
pl.add_mesh(mesh, scalar_bar_args=sargs) |
| 83 |
pl.show() |
| 84 |
|
| 85 |
|
| 86 |
# %% |
| 87 |
# Labelling values outside of the scalar range |
| 88 |
pl = pv.Plotter() |
| 89 |
pl.add_mesh( |
| 90 |
mesh, clim=[1000, 2000], below_color='blue', above_color='red', scalar_bar_args=sargs |
| 91 |
) |
| 92 |
pl.show() |
| 93 |
|
| 94 |
|
| 95 |
# %% |
| 96 |
# Annotate values of interest using a dictionary. The key of the dictionary |
| 97 |
# must be the value to annotate, and the value must be the string label. |
| 98 |
|
| 99 |
# Make a dictionary for the annotations |
| 100 |
annotations = { |
| 101 |
2300: 'High', |
| 102 |
805.3: 'Cutoff value', |
| 103 |
} |
| 104 |
|
| 105 |
pl = pv.Plotter() |
| 106 |
pl.add_mesh(mesh, scalars='Elevation', annotations=annotations) |
| 107 |
pl.show() |
| 108 |
# %% |
| 109 |
# .. tags:: plot |