add_mesh

Plotter.add_mesh(mesh, color=None, style=None, scalars=None, clim=None, show_edges=None, edge_color=None, point_size=5.0, line_width=None, opacity=1.0, flip_scalars=False, lighting=None, n_colors=256, interpolate_before_map=True, cmap=None, label=None, reset_camera=None, scalar_bar_args=None, show_scalar_bar=None, multi_colors=False, name=None, texture=None, render_points_as_spheres=None, render_lines_as_tubes=False, smooth_shading=None, ambient=0.0, diffuse=1.0, specular=0.0, specular_power=100.0, nan_color=None, nan_opacity=1.0, culling=None, rgb=None, categories=False, silhouette=False, use_transparency=False, below_color=None, above_color=None, annotations=None, pickable=True, preference='point', log_scale=False, pbr=False, metallic=0.0, roughness=0.5, render=True, component=None, **kwargs)

Add any PyVista/VTK mesh or dataset that PyVista can wrap to the scene.

This method is using a mesh representation to view the surfaces and/or geometry of datasets. For volume rendering, see pyvista.BasePlotter.add_volume().

Parameters
meshpyvista.DataSet or pyvista.MultiBlock

Any PyVista or VTK mesh is supported. Also, any dataset that pyvista.wrap() can handle including NumPy arrays of XYZ points.

colorstr or 3 item list, optional, defaults to white

Use to make the entire mesh have a single solid color. Either a string, RGB list, or hex color string. For example: color='white', color='w', color=[1, 1, 1], or color='#FFFFFF'. Color will be overridden if scalars are specified.

stylestr, optional

Visualization style of the mesh. One of the following: style='surface', style='wireframe', style='points'. Defaults to 'surface'. Note that 'wireframe' only shows a wireframe of the outer geometry.

scalarsstr or numpy.ndarray, optional

Scalars used to “color” the mesh. Accepts a string name of an array that is present on the mesh or an array equal to the number of cells or the number of points in the mesh. Array should be sized as a single vector. If both color and scalars are None, then the active scalars are used.

clim2 item list, optional

Color bar range for scalars. Defaults to minimum and maximum of scalars array. Example: [-1, 2]. rng is also an accepted alias for this.

show_edgesbool, optional

Shows the edges of a mesh. Does not apply to a wireframe representation.

edge_colorstr or 3 item list, optional, defaults to black

The solid color to give the edges when show_edges=True. Either a string, RGB list, or hex color string.

point_sizefloat, optional

Point size of any nodes in the dataset plotted. Also applicable when style=’points’. Default 5.0.

line_widthfloat, optional

Thickness of lines. Only valid for wireframe and surface representations. Default None.

opacityfloat, str, array_like

Opacity of the mesh. If a single float value is given, it will be the global opacity of the mesh and uniformly applied everywhere - should be between 0 and 1. A string can also be specified to map the scalars range to a predefined opacity transfer function (options include: ‘linear’, ‘linear_r’, ‘geom’, ‘geom_r’). A string could also be used to map a scalars array from the mesh to the opacity (must have same number of elements as the scalars argument). Or you can pass a custom made transfer function that is an array either n_colors in length or shorter.

flip_scalarsbool, optional

Flip direction of cmap. Most colormaps allow *_r suffix to do this as well.

lightingbool, optional

Enable or disable view direction lighting. Default False.

n_colorsint, optional

Number of colors to use when displaying scalars. Defaults to 256. The scalar bar will also have this many colors.

interpolate_before_mapbool, optional

Enabling makes for a smoother scalars display. Default is True. When False, OpenGL will interpolate the mapped colors which can result is showing colors that are not present in the color map.

cmapstr, list, optional

Name of the Matplotlib colormap to use when mapping the scalars. See available Matplotlib colormaps. Only applicable for when displaying scalars. Requires Matplotlib to be installed. colormap is also an accepted alias for this. If colorcet or cmocean are installed, their colormaps can be specified by name.

You can also specify a list of colors to override an existing colormap with a custom one. For example, to create a three color colormap you might specify ['green', 'red', 'blue'].

labelstr, optional

String label to use when adding a legend to the scene with pyvista.BasePlotter.add_legend().

reset_camerabool, optional

Reset the camera after adding this mesh to the scene.

scalar_bar_argsdict, optional

Dictionary of keyword arguments to pass when adding the scalar bar to the scene. For options, see pyvista.BasePlotter.add_scalar_bar().

show_scalar_barbool

If False, a scalar bar will not be added to the scene. Defaults to True.

multi_colorsbool, optional

If a MultiBlock dataset is given this will color each block by a solid color using matplotlib’s color cycler.

namestr, optional

The name for the added mesh/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.

texturevtk.vtkTexture or np.ndarray or bool, optional

A texture to apply if the input mesh has texture coordinates. This will not work with MultiBlock datasets. If set to True, the first available texture on the object will be used. If a string name is given, it will pull a texture with that name associated to the input mesh.

render_points_as_spheresbool, optional

Render points as spheres rather than dots.

render_lines_as_tubesbool, optional

Show lines as thick tubes rather than flat lines. Control the width with line_width.

smooth_shadingbool, optional

Enable smooth shading when True using either the Gouraud or Phong shading algorithm. When False, use flat shading. Automatically enabled when pbr=True.

ambientfloat, optional

When lighting is enabled, this is the amount of light in the range of 0 to 1 (default 0.0) that reaches the actor when not directed at the light source emitted from the viewer.

diffusefloat, optional

The diffuse lighting coefficient. Default 1.0.

specularfloat, optional

The specular lighting coefficient. Default 0.0.

specular_powerfloat, optional

The specular power. Between 0.0 and 128.0.

nan_colorstr or 3 item list, optional, defaults to gray

The color to use for all NaN values in the plotted scalar array.

nan_opacityfloat, optional

Opacity of NaN values. Should be between 0 and 1. Default 1.0.

cullingstr, optional

Does not render faces that are culled. Options are 'front' or 'back'. This can be helpful for dense surface meshes, especially when edges are visible, but can cause flat meshes to be partially displayed. Defaults to False.

rgbbool, optional

If an 2 dimensional array is passed as the scalars, plot those values as RGB(A) colors. rgba is also an accepted alias for this. Opacity (the A) is optional. If a scalars array ending with "_rgba" is passed, the default becomes True. This can be overridden by setting this parameter to False.

categoriesbool, optional

If set to True, then the number of unique values in the scalar array will be used as the n_colors argument.

silhouettedict, bool, optional

If set to True, plot a silhouette highlight for the mesh. This feature is only available for a triangulated PolyData. As a dict, it contains the properties of the silhouette to display:

  • color: str or 3-item list, color of the silhouette

  • line_width: float, edge width

  • opacity: float between 0 and 1, edge transparency

  • feature_angle: If a float, display sharp edges exceeding that angle in degrees.

  • decimate: float between 0 and 1, level of decimation

use_transparencybool, optional

Invert the opacity mappings and make the values correspond to transparency.

below_colorstr or 3 item list, optional

Solid color for values below the scalars range (clim). This will automatically set the scalar bar below_label to 'Below'.

above_colorstr or 3 item list, optional

Solid color for values below the scalars range (clim). This will automatically set the scalar bar above_label to 'Above'.

annotationsdict, optional

Pass a dictionary of annotations. Keys are the float values in the scalars range to annotate on the scalar bar and the values are the the string annotations.

pickablebool, optional

Set whether this actor is pickable.

preferencestr, optional

When mesh.n_points == mesh.n_cells and setting scalars, this parameter sets how the scalars will be mapped to the mesh. Default 'points', causes the scalars will be associated with the mesh points. Can be either 'points' or 'cells'.

log_scalebool, optional

Use log scale when mapping data to colors. Scalars less than zero are mapped to the smallest representable positive float. Default: True.

pbrbool, optional

Enable physics based rendering (PBR) if the mesh is PolyData. Use the color argument to set the base color. This is only available in VTK>=9.

metallicfloat, optional

Usually this value is either 0 or 1 for a real material but any value in between is valid. This parameter is only used by PBR interpolation. Default value is 0.0.

roughnessfloat, optional

This value has to be between 0 (glossy) and 1 (rough). A glossy material has reflections and a high specular part. This parameter is only used by PBR interpolation. Default value is 0.5.

renderbool, optional

Force a render when True. Default True.

componentint, optional

Set component of vector valued scalars to plot. Must be nonnegative, if supplied. If None, the magnitude of the vector is plotted.

**kwargsdict, optional

Optional developer keyword arguments.

Returns
vtk.vtkActor

VTK actor of the mesh.

Examples

Add a sphere to the plotter and show it with a custom scalar bar title.

>>> import pyvista
>>> sphere = pyvista.Sphere()
>>> sphere['Data'] = sphere.points[:, 2]
>>> plotter = pyvista.Plotter()
>>> _ = plotter.add_mesh(sphere,
...                      scalar_bar_args={'title': 'Z Position'})
>>> plotter.show()
../../../_images/pyvista-Plotter-add_mesh-1_00_00.png

Plot using RGB on a single cell. Note that since the number of points and the number of cells are identical, we have to pass preference='cell'.

>>> import pyvista
>>> import numpy as np
>>> vertices = np.array([[0, 0, 0], [1, 0, 0], [.5, .667, 0], [0.5, .33, 0.667]])
>>> faces = np.hstack([[3, 0, 1, 2], [3, 0, 3, 2], [3, 0, 1, 3], [3, 1, 2, 3]])
>>> mesh = pyvista.PolyData(vertices, faces)
>>> mesh.cell_data['colors'] = [[255, 255, 255],
...                               [0, 255, 0],
...                               [0, 0, 255],
...                               [255, 0, 0]]
>>> plotter = pyvista.Plotter()
>>> _ = plotter.add_mesh(mesh, scalars='colors', lighting=False,
...                      rgb=True, preference='cell')
>>> plotter.camera_position='xy'
>>> plotter.show()
../../../_images/pyvista-Plotter-add_mesh-1_01_00.png

Note how this varies from preference=='point'. This is because each point is now being individually colored, versus in preference=='point', each cell face is individually colored.

>>> plotter = pyvista.Plotter()
>>> _ = plotter.add_mesh(mesh, scalars='colors', lighting=False,
...                      rgb=True, preference='point')
>>> plotter.camera_position='xy'
>>> plotter.show()
../../../_images/pyvista-Plotter-add_mesh-1_02_00.png