add_legend

Plotter.add_legend(labels=None, bcolor=(0.5, 0.5, 0.5), border=False, size=None, name=None, origin=None, face=None)

Add a legend to render window.

Entries must be a list containing one string and color entry for each item.

Parameters
labelslist, optional

When set to None, uses existing labels as specified by

  • add_mesh

  • add_lines

  • add_points

List containing one entry for each item to be added to the legend. Each entry must contain two strings, [label, color], where label is the name of the item to add, and color is the color of the label to add.

bcolorlist or str, optional

Background color, either a three item 0 to 1 RGB color list, or a matplotlib color string (e.g. ‘w’ or ‘white’ for a white color). If None, legend background is disabled.

borderbool, optional

Controls if there will be a border around the legend. Default False.

sizelist, optional

Two float list, each float between 0 and 1. For example [0.1, 0.1] would make the legend 10% the size of the entire figure window.

namestr, optional

The name for the added actor so that it can be easily updated. If an actor of this name already exists in the rendering window, it will be replaced by the new actor.

originlist, optional

If used, specifies the x and y position of the lower left corner of the legend.

facestr, optional

Face shape of legend face. Accepted options:

  • 'triangle'

  • 'circle'

  • 'rectangle'

Default is 'triangle'.

Returns
vtk.vtkLegendBoxActor

Actor for the legend.

Examples

>>> import pyvista
>>> from pyvista import examples
>>> mesh = examples.load_hexbeam()
>>> othermesh = examples.load_uniform()
>>> plotter = pyvista.Plotter()
>>> _ = plotter.add_mesh(mesh, label='My Mesh')
>>> _ = plotter.add_mesh(othermesh, 'k', label='My Other Mesh')
>>> _ = plotter.add_legend()
>>> plotter.show()
../../../_images/pyvista-Plotter-add_legend-1_00_00.png

Alternative manual example

>>> import pyvista
>>> from pyvista import examples
>>> mesh = examples.load_hexbeam()
>>> othermesh = examples.load_uniform()
>>> legend_entries = []
>>> legend_entries.append(['My Mesh', 'w'])
>>> legend_entries.append(['My Other Mesh', 'k'])
>>> plotter = pyvista.Plotter()
>>> _ = plotter.add_mesh(mesh)
>>> _ = plotter.add_mesh(othermesh, 'k')
>>> _ = plotter.add_legend(legend_entries)
>>> plotter.show()
../../../_images/pyvista-Plotter-add_legend-1_01_00.png