{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plotting Glyphs (Vectors or PolyData)\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": [
        "pl = pv.Plotter()\npl.add_mesh(arrows, color=\"black\")\npl.add_mesh(mesh, scalars=\"Elevation\", cmap=\"terrain\", smooth_shading=True)\npl.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": [
        "pl = pv.Plotter()\npl.add_mesh(sphere.arrows, lighting=False, scalar_bar_args={\"title\": \"Vector Magnitude\"})\npl.add_mesh(sphere, color=\"grey\", ambient=0.6, opacity=0.5, show_edges=False)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "```{=html}\n<center>\n  <a target=\"_blank\" href=\"https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/04_filters/exercises/e_glyphs.ipynb\">\n    <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/ width=\"150px\">\n  </a>\n</center>\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}