| 1 |
""" |
| 2 |
.. _legend_example: |
| 3 |
|
| 4 |
Legends and glyphs |
| 5 |
~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Using custom legends and glyphs within PyVista. |
| 8 |
""" |
| 9 |
|
| 10 |
import pyvista as pv |
| 11 |
from pyvista import examples |
| 12 |
|
| 13 |
# sphinx_gallery_start_ignore |
| 14 |
# labels are not currently supported by vtk-js |
| 15 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 16 |
# sphinx_gallery_end_ignore |
| 17 |
|
| 18 |
# sphinx_gallery_thumbnail_number = 3 |
| 19 |
|
| 20 |
# %% |
| 21 |
# Default legend for glyphs |
| 22 |
# +++++++++++++++++++++++++ |
| 23 |
# |
| 24 |
# The method :func:`~pyvista.Plotter.add_legend` is able to retrieve and use |
| 25 |
# the glyphs for each plot. |
| 26 |
# |
| 27 |
pl = pv.Plotter() |
| 28 |
|
| 29 |
# Loading mesh |
| 30 |
mesh = examples.load_random_hills() |
| 31 |
|
| 32 |
# Adding another set of glyphs with opposite value. |
| 33 |
mesh['NormalInversed'] = -1 * mesh['Normals'].copy() |
| 34 |
|
| 35 |
# Generating glyph meshes |
| 36 |
arrows = mesh.glyph(scale='Normals', orient='Normals', tolerance=0.05) |
| 37 |
pl.add_mesh(arrows, color='blue', label='Top pressure') |
| 38 |
|
| 39 |
arrows_inversed = mesh.glyph( |
| 40 |
scale='NormalInversed', orient='NormalInversed', tolerance=0.05 |
| 41 |
) |
| 42 |
pl.add_mesh(arrows_inversed, color='red', label='Lower pressure') |
| 43 |
|
| 44 |
# Adding surface mesh |
| 45 |
pl.add_mesh(mesh, scalars='Elevation', cmap='terrain', show_scalar_bar=False) |
| 46 |
|
| 47 |
# Add default legend |
| 48 |
pl.add_legend() |
| 49 |
|
| 50 |
# Plot |
| 51 |
pl.show() |
| 52 |
|
| 53 |
# %% |
| 54 |
# Using custom legends |
| 55 |
# ++++++++++++++++++++ |
| 56 |
# |
| 57 |
# You can use specific labels with :func:`~pyvista.Plotter.add_legend` |
| 58 |
# |
| 59 |
|
| 60 |
pl = pv.Plotter() |
| 61 |
|
| 62 |
# Adding glyph meshes |
| 63 |
pl.add_mesh(arrows, color='blue', label='Top pressure') |
| 64 |
pl.add_mesh(arrows_inversed, color='red', label='Lower pressure') |
| 65 |
|
| 66 |
# Adding surface mesh |
| 67 |
pl.add_mesh(mesh, scalars='Elevation', cmap='terrain', show_scalar_bar=False) |
| 68 |
|
| 69 |
# adding legend |
| 70 |
legend = ['New top pressure', 'New lower pressure'] |
| 71 |
|
| 72 |
# You can |
| 73 |
pl.add_legend(legend) |
| 74 |
|
| 75 |
# In this case, the default values are used, not the ones from the |
| 76 |
# :func:`~pyvista.Plotter.add_mesh`. |
| 77 |
|
| 78 |
pl.show() |
| 79 |
|
| 80 |
|
| 81 |
# %% |
| 82 |
# Using custom legend and glyphs |
| 83 |
# ++++++++++++++++++++++++++++++ |
| 84 |
# |
| 85 |
# You can use specific labels or glyphs even if they have been specified before. |
| 86 |
# |
| 87 |
pl = pv.Plotter() |
| 88 |
|
| 89 |
# Adding glyph meshes |
| 90 |
pl.add_mesh(arrows, color='blue', label='Top pressure') |
| 91 |
pl.add_mesh(arrows_inversed, color='red', label='Lower pressure') |
| 92 |
|
| 93 |
# Adding surface mesh |
| 94 |
pl.add_mesh(mesh, scalars='Elevation', cmap='terrain', show_scalar_bar=False) |
| 95 |
|
| 96 |
# You can use custom glyphs even if they don't match the plot. |
| 97 |
# You can use either ``list`` or ``dict`` for passing the data. |
| 98 |
legend = [ |
| 99 |
['Top pressure', 'blue'], # no custom glyph |
| 100 |
['Middle point pressure', 'green', 'circle'], # Using a defaults glyph |
| 101 |
{'label': 'Lower pressure', 'color': 'red', 'face': pv.Box()}, |
| 102 |
] |
| 103 |
|
| 104 |
# You can |
| 105 |
pl.add_legend(legend) |
| 106 |
|
| 107 |
# Plot |
| 108 |
pl.show() |
| 109 |
# %% |
| 110 |
# .. tags:: plot |