Control Global and Local Plotting Themes

PyVista allows you to set global and local plotting themes to easily set default plotting parameters.

import pyvista as pv
from pyvista import examples

Define a simple plotting routine for comparing the themes.

mesh = examples.download_st_helens().warp_by_scalar()


def plot_example():
    p = pv.Plotter()
    p.add_mesh(mesh)
    p.add_bounding_box()
    p.show()

PyVista’s default color theme is chosen to be generally easy on your eyes and is best used when working long hours on your visualization project. The grey background and warm colormaps are chosen to make sure 3D renderings do not drastically change the brightness of your screen when working in dark environments.

Here’s an example of our default plotting theme - this is what you would see by default after running any of our examples locally.

pv.set_plot_theme('default')
plot_example()
themes

PyVista also ships with a few plotting themes:

  • 'ParaView': this is designed to mimic ParaView’s default plotting theme.

  • 'dark': this is designed to be night-mode friendly with dark backgrounds and color schemes.

  • 'document': this is built for use in document style plotting and making publication quality figures.

Demo the 'ParaView' theme.

pv.set_plot_theme("paraview")

plot_example()
themes

Demo the 'dark' theme.

pv.set_plot_theme("dark")

plot_example()
themes

Demo the 'document' theme. This theme is used on our online examples.

pv.set_plot_theme("document")

plot_example()
themes

Note that you can also use color gradients for the background of the plotting window!

plotter = pv.Plotter()
plotter.add_mesh(mesh)
plotter.show_grid()
# Here we set the gradient
plotter.set_background("royalblue", top="aliceblue")
cpos = plotter.show()
themes

Modifying the Global Theme

You can control how meshes are displayed by setting individual parameters when plotting like mesh.plot(show_edges=True), or by setting a global theme. You can also control individual parameters how all meshes are displayed by default via pyvista.global_theme.

Here, we print out the current global defaults for all pyvista meshes. These values have been changed by the previous “Document” theme.

pv.global_theme

Out:

Document Theme
--------------
Background               : (1.0, 1.0, 1.0)
Jupyter backend          : ipyvtklink
Full screen              : False
Window size              : [1024, 768]
Camera                   : {'position': [1, 1, 1], 'viewup': [0, 0, 1]}
Notebook                 : None
Font                     :
    Family               : arial
    Size                 : 18
    Title size           : 18
    Label size           : 18
    Color                : (0.0, 0.0, 0.0)
    Float format         : None
Auto close               : True
Colormap                 : viridis
Color                    : (0.8235294117647058, 0.7058823529411765, 0.5490196078431373)
NaN color                : (0.6627450980392157, 0.6627450980392157, 0.6627450980392157)
Edge color               : (0.0, 0.0, 0.0)
Outline color            : (0.0, 0.0, 0.0)
Floor color              : (0.5019607843137255, 0.5019607843137255, 0.5019607843137255)
Colorbar orientation     : horizontal
Colorbar - horizontal    :
    Width                : 0.6
    Height               : 0.08
    X Position           : 0.35
    Y Position           : 0.05
Colorbar - vertical      :
    Width                : 0.08
    Height               : 0.45
    X Position           : 0.9
    Y Position           : 0.02
Show scalar bar          : True
Show edges               : False
Lighting                 : True
Interactive              : False
Render points as spheres : False
Transparent Background   : False
Title                    : PyVista
Axes                     : Axes configuration
    X Color              : (1.0, 0.38823529411764707, 0.2784313725490196)
    Y Color              : (0.1803921568627451, 0.5450980392156862, 0.3411764705882353)
    Z Color              : (0.0, 0.0, 1.0)
    Use Box              : False
    Show                 : True
Multi-samples            : 4
Multi-renderer Split Pos : None
Volume mapper            : smart
Smooth shading           : False
Depth peeling            :
    Number               : 4
    Occlusion ratio      : 0.0
    Enabled              : False
Silhouette               :
    Color                : (0.0, 0.0, 0.0)
    Line width           : 2
    Opacity              : 1.0
    Feature angle        : None
    Decimate             : 0.9
Slider Styles            :
    Classic              :
        Slider length    : 0.02
        Slider width     : 0.04
        Slider color     : (0.5, 0.5, 0.5)
        Tube width       : 0.005
        Tube color       : (1, 1, 1)
        Cap opacity      : 1.0
        Cap length       : 0.01
        Cap width        : 0.02
    Modern               :
        Slider length    : 0.02
        Slider width     : 0.04
        Slider color     : (0.43137255, 0.44313725, 0.45882353)
        Tube width       : 0.04
        Tube color       : (0.69803922, 0.70196078, 0.70980392)
        Cap opacity      : 0.0
        Cap length       : 0.01
        Cap width        : 0.02

By default, edges are not shown on meshes unless explicitly specified when plotting a mesh via show_edges=True. You can change this default behavior globally by changing the default parameter.

pv.global_theme.show_edges = True
cpos = pv.Sphere().plot()
themes

You can reset pyvista to default behavior with restore_defaults. Note that the figure’s color was reset to the default “white” color rather than the “tan” color default with the document theme. Under the hood, each theme applied changes the global plot defaults stored within pyvista.global_theme.

pv.global_theme.restore_defaults()
cpos = pv.Sphere().plot()
themes

Creating a Custom Theme and Applying it Globally

You can create a custom theme by modifying one of the existing themes and then loading it into the global plotting defaults.

Here, we create a dark theme that plots meshes red by default while showing edges.

from pyvista import themes

my_theme = themes.DarkTheme()
my_theme.color = 'red'
my_theme.lighting = False
my_theme.show_edges = True
my_theme.axes.box = True

pv.global_theme.load_theme(my_theme)
cpos = pv.Sphere().plot()
themes

Creating a Custom Theme and Applying it to a Single Plotter

In this example, we create a custom theme from the base “default” theme and then apply it to a single plotter. Note that this does not change the behavior of the global “defaults”, which are still set to the modified DarkTheme.

This approach carries the advantage that you can maintain several themes and apply them to one or more plotters.

from pyvista import themes

my_theme = themes.DefaultTheme()
my_theme.color = 'black'
my_theme.lighting = True
my_theme.show_edges = True
my_theme.edge_color = 'white'
my_theme.background = 'white'

cpos = pv.Sphere().plot(theme=my_theme)
themes

Alternatively, set the theme of an instance of Plotter.

pl = pv.Plotter(theme=my_theme)
# pl.theme = my_theme  # alternatively use the setter
pl.add_mesh(pv.Cube())
cpos = pl.show()
themes

Reset to use the document theme

pv.set_plot_theme("document")

Total running time of the script: ( 0 minutes 6.094 seconds)

Gallery generated by Sphinx-Gallery