.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/mesh/polygon_animation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_mesh_polygon_animation.py: Polygon animation ================= Polygon animation example that changes the polygon data. Random points are generated by sampling from a 2D gaussian and a polygon is updated to visualize a convex hull for the sampled points. .. GENERATED FROM PYTHON SOURCE LINES 9-76 .. image-sg:: /_gallery/mesh/images/sphx_glr_polygon_animation_001.webp :alt: polygon animation :srcset: /_gallery/mesh/images/sphx_glr_polygon_animation_001.webp :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/fastplotlib/fastplotlib/fastplotlib/graphics/features/_base.py:18: UserWarning: casting float64 array to float32 warn(f"casting {array.dtype} array to float32") | .. code-block:: Python # test_example = false import numpy as np from scipy.spatial import ConvexHull import fastplotlib as fpl def points_to_hull(points) -> np.ndarray: hull = ConvexHull(points, qhull_options="Qs") return points[hull.vertices] figure = fpl.Figure(size=(700, 560)) cov = np.array([[1, 0], [0, 1]]) # sample points from a 2d gaussian samples1 = np.random.multivariate_normal((0, 0), cov, size=20) samples2 = np.random.multivariate_normal((5, 0), cov, size=50) # add the convex hull as a polygon polygon1 = figure[0, 0].add_polygon( points_to_hull(samples1), colors="cyan", alpha=0.7, alpha_mode="blend" ) # add the sampled points scatter1 = figure[0, 0].add_scatter( samples1, sizes=8, colors="blue", alpha=0.7, alpha_mode="blend" ) # add the second gaussian and convex hull polygon polygon2 = figure[0, 0].add_polygon( points_to_hull(samples2), colors="magenta", alpha=0.7, alpha_mode="blend" ) scatter2 = figure[0, 0].add_scatter( samples2, sizes=8, colors="r", alpha=0.7, alpha_mode="blend" ) def animate(): # set new scatter data scatter1.data[:, :-1] += np.random.normal(0, 0.05, size=samples1.size).reshape( samples1.shape ) # set convex hull with new polygon vertices polygon1.data = points_to_hull(scatter1.data[:, :-1]) # set the other scatter and polygon scatter2.data[:, :-1] += np.random.normal(0, 0.05, size=samples2.size).reshape( samples2.shape ) polygon2.data = points_to_hull(scatter2.data[:, :-1]) figure.show() figure[0, 0].camera.width = 10 figure[0, 0].camera.height = 10 figure.add_animations(animate) # NOTE: fpl.loop.run() should not be used for interactive sessions # See the "JupyterLab and IPython" section in the user guide if __name__ == "__main__": print(__doc__) fpl.loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 18.397 seconds) .. _sphx_glr_download__gallery_mesh_polygon_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: polygon_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: polygon_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_