{ "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": [ "Plotting Glyphs (Vectors or PolyData)\n=====================================\n\nUse vectors in a dataset to plot and orient glyphs/geometric objects.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example dataset with normals\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.load_random_hills()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Glyphying can be done via the\n`pyvista.DataSetFilters.glyph`{.interpreted-text role=\"func\"} filter\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "help(mesh.glyph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you might not want glyphs for every node in the input dataset.\nIn this case, you can choose to build glyphs for a subset of the input\ndataset by using a merging tolerance. Here we specify a merging\ntolerance of five percent which equates to five percent of the bounding\nbox\\'s length.\n\ncreate a subset of arrows using the glyph filter\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "arrows = ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(arrows, color=\"black\")\np.add_mesh(mesh, scalars=\"Elevation\", cmap=\"terrain\", smooth_shading=True)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A common approach is to load vectors directly to the mesh object and\nthen access the `pyvista.DataSet.arrows`{.interpreted-text role=\"attr\"}\nproperty to produce glyphs.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sphere = pv.Sphere(radius=3.14)\n\n# make cool swirly pattern\nvectors = np.vstack(\n (\n np.sin(sphere.points[:, 0]),\n np.cos(sphere.points[:, 1]),\n np.cos(sphere.points[:, 2]),\n )\n).T\nvectors" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# add and scale\nsphere[\"vectors\"] = vectors * 0.3\nsphere.set_active_vectors(\"vectors\")\n\n# plot just the arrows\nsphere.arrows.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the arrows and the sphere.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(sphere.arrows, lighting=False, scalar_bar_args={'title': \"Vector Magnitude\"})\np.add_mesh(sphere, color=\"grey\", ambient=0.6, opacity=0.5, show_edges=False)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n