.. 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-41 .. code-block:: Python from gridkit import HexGrid from gridkit.doc_utils import plot_polygons # create a grid main_grid = HexGrid(size=10, shape="pointy") target_loc = (0, 0) def get_cells_around_target(grid): target_cell = grid.cell_at_point(target_loc) ids = grid.neighbours(target_cell, depth=3 * 10 / grid.size) shapes = grid.to_shapely(ids) return shapes # define a location around which we want to obtain some cells main_shapes = get_cells_around_target(main_grid) .. GENERATED FROM PYTHON SOURCE LINES 43-45 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 46-52 .. code-block:: Python import matplotlib.pyplot as plt plot_polygons(main_shapes, linewidth=2, fill=False, colors="orange") 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 53-61 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 62-73 .. code-block:: Python main_grid.offset = (0, main_grid.dy / 2) main_shapes = get_cells_around_target(main_grid) # plot the cell outlines plot_polygons(main_shapes, linewidth=2, fill=False, colors="orange") 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 74-79 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 80-95 .. code-block:: Python shifted_grid = main_grid.update( offset=(main_grid.dx / 2, (main_grid.dy + main_grid.r) / 2) ) shifted_shapes = get_cells_around_target( shifted_grid, ) # plot the cell outlines plot_polygons(main_shapes, linewidth=2, fill=False, colors="orange") plot_polygons(shifted_shapes, linewidth=2, fill=False, colors="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 96-108 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. Even more convenient is that we can shift the grid after creating it using the .anchor() method. That way we don't even have to get clever ourselves. 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 109-125 .. code-block:: Python half_size_grid = main_grid.update(size=main_grid.size / 2).anchor(target_loc) third_size_grid = main_grid.update(size=main_grid.size / 3).anchor(target_loc) half_size_shapes = get_cells_around_target(half_size_grid) third_size_shapes = get_cells_around_target(third_size_grid) # plot the cell outlines plot_polygons(third_size_shapes, linewidth=2, fill=False, colors="purple") plot_polygons(half_size_shapes, linewidth=2, fill=False, colors="red") plot_polygons(main_shapes, linewidth=2, fill=False, colors="orange") plt.xlim(-15, 15) plt.ylim(-15, 15) 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 126-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.278 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 `_