{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import subprocess\nimport sys\n\nif \"google.colab\" in sys.modules:\n subprocess.run(\"apt-get update\", shell=True, check=True)\n subprocess.run(\"apt-get install -qq xvfb libgl1-mesa-glx\", shell=True, check=True)\n subprocess.run(\"pip install pyvista[all] -qq\", shell=True, check=True)\n\n import pyvista as pv\n\n # Seems that only static plotting is supported by colab at the moment\n pv.global_theme.jupyter_backend = \"static\"\n pv.global_theme.notebook = True\n pv.start_xvfb()\nelse:\n %matplotlib inline\n from pyvista import set_plot_theme\n\n set_plot_theme(\"document\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lesson Overview\n===============\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv\nfrom pyvista import examples\n\nmesh = pv.Wavelet()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`add_mesh`\n==========\n\nWhen plotting, users must first create a\n`pyvista.Plotter`{.interpreted-text role=\"class\"} instance (much like a\nMatplotlib figure). Then data are added to the plotter instance through\nthe `pyvista.Plotter.add_mesh`{.interpreted-text role=\"func\"} method.\nThis workflow typically looks like:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(mesh)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can customize how that mesh is displayed through the parameters of\nthe `pyvista.Plotter.add_mesh`{.interpreted-text role=\"func\"} method.\nFor example, we can change the colormap via the `cmap` argument:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(mesh, cmap='coolwarm')\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or show the edges of the mesh with `show_edges`:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(mesh, show_edges=True)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or adjust the opacity to be a scalar value or linear transfer function\nvia the `opacity` argument:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_st_helens().warp_by_scalar()\n\np = pv.Plotter()\np.add_mesh(mesh, cmap='terrain', opacity=\"linear\")\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look at all of the options for\n[add\\_mesh](https://docs.pyvista.org/api/plotting/_autosummary/pyvista.Plotter.add_mesh.html).\n\nThe `add_mesh` method can be called over and over to add different data\nto the same `Plotter` scene. For example, we can create many different\nmesh objects and plot them together:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "kinds = [\n 'tetrahedron',\n 'cube',\n 'octahedron',\n 'dodecahedron',\n 'icosahedron',\n]\ncenters = [\n (0, 1, 0),\n (0, 0, 0),\n (0, 2, 0),\n (-1, 0, 0),\n (-1, 2, 0),\n]\n\nsolids = [pv.PlatonicSolid(kind, radius=0.4, center=center) for kind, center in zip(kinds, centers)]\n\np = pv.Plotter(window_size=[1000, 1000])\nfor ind, solid in enumerate(solids):\n p.add_mesh(solid, color='silver', specular=1.0, specular_power=10)\np.view_vector((5.0, 2, 3))\np.add_floor('-z', lighting=True, color='tan', pad=1.0)\np.enable_shadows()\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplotting\n===========\n\nCreating side-by-side comparisons of datasets is easy with PyVista\\'s\nsubplotting API. Get started by specifying the shape of the\n`pyvista.Plotter`{.interpreted-text role=\"class\"} object then\nregistering the active subplot by the\n`pyvista.Plotter.subplot`{.interpreted-text role=\"func\"} method much\nlike how you subplot with Matplotlib\\'s API.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter(shape=(1, 2))\n\np.subplot(0, 0)\np.add_mesh(pv.Sphere())\n\np.subplot(0, 1)\np.add_mesh(pv.Cube())\n\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below is an example of side-by-side comparisons of the contours and\nslices of a single dataset.\n\n::: {.tip}\n::: {.title}\nTip\n:::\n\nYou can link the cameras of both views with the\n`pyvista.Plotter.link_views`{.interpreted-text role=\"func\"} method\n:::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pv.Wavelet()\ncntr = mesh.contour()\nslices = mesh.slice_orthogonal()\n\np = pv.Plotter(shape=(1, 2))\n\np.add_mesh(cntr)\n\np.subplot(0, 1)\np.add_mesh(slices)\n\np.link_views()\np.view_isometric()\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Axes and Bounds\n===============\n\nAxes can be added to the scene with\n`pyvista.Plotter.show_axes`{.interpreted-text role=\"func\"}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.load_random_hills()\n\np = pv.Plotter()\np.add_mesh(mesh)\np.show_axes()\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And bounds similarly with\n`pyvista.Plotter.show_bounds`{.interpreted-text role=\"func\"}\n\n::: {.tip}\n::: {.title}\nTip\n:::\n\nSee [Plotting\nBounds](https://docs.pyvista.org/examples/02-plot/bounds.html) for more\ndetails.\n:::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(mesh)\np.show_axes()\np.show_bounds()\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n