.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/grid_definitions/hex_grids.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_hex_grids.py: .. _example hexagon grids: Hexagon grids ============= Adjusting shapes and sizes Introduction ------------ In this example a variety of hexagonal grids is created and compared. The goal of this exercise is to familiarize the reader with the various configurations of hexagonal grids and how they compare to one another. Let's create a grid and obtain the polygons in some area of interest. .. GENERATED FROM PYTHON SOURCE LINES 19-39 .. code-block:: Python from gridkit import HexGrid # create a grid main_grid = HexGrid(size=10, shape="pointy") # define an area of interest, slightly larger than what we want to plot bounds = (-20, -20, 20, 20) def get_shapes_in_bounds(grid, bounds): """Return the cells of a grid in the specified bounds as a multipolygon""" aligned_bounds = grid.align_bounds(bounds) cell_ids = grid.cells_in_bounds(aligned_bounds) return grid.to_shapely(cell_ids, as_multipolygon=True) main_shapes = get_shapes_in_bounds(main_grid, bounds) .. GENERATED FROM PYTHON SOURCE LINES 41-43 Now we have shapes representing some of the cells in the grid. Let's plot the shapes to see what we are dealing with. .. GENERATED FROM PYTHON SOURCE LINES 44-58 .. code-block:: Python import matplotlib.pyplot as plt def plot_shapes(shapes, color="orange", **kwargs): """Simple function to plot polygons with matplotlib""" for geom in shapes.geoms: plt.plot(*geom.exterior.xy, color=color, **kwargs) # plot the cell outlines plot_shapes(main_shapes, linewidth=2) plt.grid() plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_001.png :alt: hex grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-67 In this plot the gridlines are shown in the background to highlight how the grid is positioned in space We can modify this using the 'offset'. Shifting grids -------------- Let's shift the whole grid vertically to center it around coordinate (0,0) Also, let's zoom in a bit to the center .. GENERATED FROM PYTHON SOURCE LINES 68-79 .. code-block:: Python main_grid.offset = (0, main_grid.dy / 2) main_shapes = get_shapes_in_bounds(main_grid, bounds) # plot the cell outlines plot_shapes(main_shapes, linewidth=2) plt.xlim(-15, 15) plt.ylim(-15, 15) plt.grid() plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_002.png :alt: hex grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-85 We can create a second grid and play with the size and offset If we shift the grid a deliberate amount so that it has a vertex on the center, it creates interesting patterns with respect to the grid we just created. For this we need to shift it half a 'dx' horizontally and half a radius (distance from center to corner) vertically, with respect to the main grid. .. GENERATED FROM PYTHON SOURCE LINES 86-99 .. code-block:: Python shifted_grid = main_grid.update( offset=(main_grid.dx / 2, (main_grid.dy + main_grid.r) / 2) ) shifted_shapes = get_shapes_in_bounds(shifted_grid, bounds) # plot the cell outlines plot_shapes(main_shapes, linewidth=2) plot_shapes(shifted_shapes, linewidth=2, color="purple") plt.xlim(-15, 15) plt.ylim(-15, 15) plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_003.png :alt: hex grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-110 Here we were able to use the dimensions of the already existing main_grid to determine the offset of the shifted grid. It is often more convenient to first create a grid and then shift it, than it is to calculate the desired shift beforehand. The advantage of determining the offset later is that we can use the already available `dx`, `dy` and `r` properties of the created grid. Grid sizes ---------- Finally, let's play with the size of the grid. If we keep the grids centered around zero but vary the size, we also obtain interesting patterns. .. GENERATED FROM PYTHON SOURCE LINES 111-126 .. code-block:: Python half_size_grid = main_grid.update(size=main_grid.size / 2).anchor([0, 0]) third_size_grid = main_grid.update(size=main_grid.size / 3).anchor([0, 0]) half_size_shapes = get_shapes_in_bounds(half_size_grid, bounds) third_size_shapes = get_shapes_in_bounds(third_size_grid, bounds) # plot the cell outlines plot_shapes(third_size_shapes, linewidth=2, color="purple") plot_shapes(half_size_shapes, linewidth=2, color="red") plot_shapes(main_shapes, linewidth=2) plt.xlim(-12, 12) plt.ylim(-12, 12) plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_004.png :alt: hex grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_hex_grids_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-129 Grids that overlap and intersect in predictable ways can be utilized to select particular cells of interest. See :ref:`example selecting cells` for a example where this is done. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.383 seconds) .. _sphx_glr_download_example_gallery_grid_definitions_hex_grids.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: hex_grids.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: hex_grids.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: hex_grids.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_