{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Point Clouds {#plotting_point_clouds}\n\nThis example shows you how to plot point clouds using PyVista using both\nthe `'points'` and `'points_gaussian'` styles.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compare the Plotting methods\n\nFirst, let\\'s create a sample point cloud using\n`numpy.random.random`{.interpreted-text role=\"func\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rng = np.random.default_rng()\npoints = rng.random((1000, 3))\npoints" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Plot\n\nWe can simply plot this point cloud using the convenience\n`pyvista.plot`{.interpreted-text role=\"func\"} function.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.plot(points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plot with Scalars\n\nThat\\'s quite boring, so let\\'s spice things up by adding color. We can\neither use a single scalar to plot the points. For example, the z\ncoordinates.\n\nFor fun, let\\'s also render the points as spheres.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.plot(\n points,\n scalars=points[:, 2],\n render_points_as_spheres=True,\n point_size=20,\n show_scalar_bar=False,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plot with RGBA\n\nAlternatively, we can color the point cloud using an RGBA array. This\nhas been normalized from (0, 1), but we could have also used a\n`numpy.uint8` array from 0-255.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rgba = points - points.min(axis=0)\nrgba /= rgba.max(axis=0)\npv.plot(points, scalars=rgba, render_points_as_spheres=True, point_size=20, cpos='xy', rgba=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Point Cloud Plot Styles\n\nPyVista supports the `'points_gaussian'` style, which renders points as\nindividual soft sprites. You have the option of displaying these as\ntight \\\"spheres\\\" using `render_points_as_spheres=True` (default), or\ndisabling it to create softer points at the expense of render\nperformance.\n\nHere\\'s the basic plot again, but with the style as `'points_gaussian'`:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.plot(points, style='points_gaussian', opacity=0.5, point_size=15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here\\'s a plotter with four combinations of the options side-by-side so\nyou can see for yourself the different options available when plotting\nthese points. PyVista tries to achieve sensible defaults, but should you\nfind these insufficient for your needs, feel free to play around with\nthe various options and find something that works for you.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl = pv.Plotter(shape=(2, 2))\n\n# Standard points\nactor = pl.add_points(\n points,\n style='points',\n emissive=False,\n scalars=rgba,\n rgba=True,\n point_size=10,\n ambient=0.7,\n)\npl.add_text('\"points\" not as spheres', color='w')\n\n# Gaussian points\npl.subplot(0, 1)\nactor = pl.add_points(\n points,\n render_points_as_spheres=False,\n style='points_gaussian',\n emissive=False,\n scalars=rgba,\n rgba=True,\n opacity=0.99,\n point_size=10,\n ambient=1.0,\n)\npl.add_text('\"points_gaussian\" not as spheres\\nemissive=False', color='w')\n\n# Gaussian points with emissive=True\npl.subplot(1, 0)\nactor = pl.add_points(\n points,\n render_points_as_spheres=False,\n style='points_gaussian',\n emissive=True,\n scalars=rgba,\n rgba=True,\n point_size=10,\n)\npl.add_text('\"points_gaussian\" not as spheres\\nemissive=True', color='w')\n\n# With render_points_as_spheres=True\npl.subplot(1, 1)\nactor = pl.add_points(\n points,\n style='points_gaussian',\n render_points_as_spheres=True,\n scalars=rgba,\n rgba=True,\n point_size=10,\n)\npl.add_text('\"points_gaussian\" as spheres', color='w')\n\npl.background_color = 'k'\npl.link_views()\npl.camera_position = 'xy'\npl.camera.zoom(1.2)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Orbit a Point Cloud\n\nGenerate a plot orbiting around a point cloud. Color based on the\ndistance from the center of the cloud.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cloud = examples.download_cloud_dark_matter()\nscalars = np.linalg.norm(cloud.points - cloud.center, axis=1)\n\npl = pv.Plotter(off_screen=True)\npl.add_mesh(\n cloud,\n style='points_gaussian',\n color='#fff7c2',\n scalars=scalars,\n opacity=0.25,\n point_size=4.0,\n show_scalar_bar=False,\n)\npl.background_color = 'k'\npl.show(auto_close=False)\npath = pl.generate_orbital_path(n_points=36, shift=cloud.length, factor=3.0)\npl.open_gif(\"orbit_cloud.gif\")\npl.orbit_on_path(path, write_frames=True)\npl.close()" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }