.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/grid_definitions/rotation_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_example_gallery_grid_definitions_rotation_animation.py: .. _example rotated animation: Rotation animation ================== Animated visualization of grid rotation Introduction ------------ Grids can be rotated around the origin. Some more info on rotations is in example :ref:`rotated_grids.py `. In this example, an animation is created where the grid rotates around the origin. The grid is centered around zero and rotated a little more every frame. Since hexagonal grids have a six-fold symmetry, we only have to animate from 0-60 and loop it. It will look like a continuous rotation. Every frame, a new grid is created that is centered around 0,0 and rotated two degrees more than the previous frame. .. GENERATED FROM PYTHON SOURCE LINES 24-59 .. container:: sphx-glr-animation .. raw:: html
.. code-block:: Python import matplotlib.pyplot as plt import numpy from matplotlib.animation import FuncAnimation from gridkit import HexGrid from gridkit.doc_utils import plot_polygons # Initialize grid = HexGrid(size=1, rotation=0).anchor([0, 0]) center_id = grid.cell_at_point([0, 0]) ids = grid.neighbours(center_id, depth=4) distance = numpy.linalg.norm(grid.centroid(ids) - grid.centroid(center_id), axis=1) def update_frame(rotation): ax.clear() ax.scatter(0, 0) grid.rotation = rotation geoms = grid.to_shapely(ids, as_multipolygon=True) im = plot_polygons(geoms.geoms, colors=distance, fill=True, ax=ax) ax.set_title(f"Rotation: {rotation} degrees") ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_axis_off() # Create animation fig, ax = plt.subplots() ax.set_aspect("equal") anim = FuncAnimation( fig, update_frame, frames=range(0, 60, 2), repeat=True, interval=50 ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.106 seconds) .. _sphx_glr_download_example_gallery_grid_definitions_rotation_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: rotation_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: rotation_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: rotation_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_