networkx.generators.lattice.hexagonal_lattice_graph

hexagonal_lattice_graph(m, n, periodic=False, with_positions=True, create_using=None)[source]

Returns an m by n hexagonal lattice graph.

The hexagonal lattice graph is a graph whose nodes and edges are the hexagonal tiling of the plane.

The returned graph will have m rows and n columns of hexagons. Odd numbered columns are shifted up relative to even numbered columns.

Positions of nodes are computed by default or with_positions is True. Node positions creating the standard embedding in the plane with sidelength 1 and are stored in the node attribute ‘pos’. pos = nx.get_node_attributes(G, 'pos') creates a dict ready for drawing.

Parameters
  • m (int) – The number of rows of hexagons in the lattice.

  • n (int) – The number of columns of hexagons in the lattice.

  • periodic (bool) – Whether to make a periodic grid by joining the boundary vertices. For this to work n must be odd and both n > 1 and m > 1. The periodic connections create another row and column of hexagons so these graphs have fewer nodes as boundary nodes are identified.

  • with_positions (bool (default: True)) – Store the coordinates of each node in the graph node attribute ‘pos’. The coordinates provide a lattice with vertical columns of hexagons offset to interleave and cover the plane. Periodic positions shift the nodes vertically in a nonlinear way so the edges don’t overlap so much.

  • create_using (NetworkX graph constructor, optional (default=nx.Graph)) – Graph type to create. If graph instance, then cleared before populated. If graph is directed, edges will point up or right.

Returns

The m by n hexagonal lattice graph.

Return type

NetworkX graph