File size: 1,323 Bytes
8b82e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon


## set texto over lattice grid
def hex_lattice(text, size=1, figsize=(8, 8)):
    fig, ax = plt.subplots(figsize=figsize)
    ax.set_aspect('equal')
    ax.axis('off')

    # Define hexagonal lattice parameters
    radius = size * np.sqrt(3) / 2
    x_offset = size * 1.5
    y_offset = size * np.sqrt(3)

    # Function to plot hexagon
    def hexagon(x, y, color='white'):
        hexagon = RegularPolygon((x, y), numVertices=6, radius=radius, orientation=np.pi/2, facecolor=color, edgecolor='black')
        ax.add_patch(hexagon)

    # Generate lattice grid
    rows = len(text)
    cols = max(len(row) for row in text)
    for r in range(rows):
        for c in range(cols):
            x = c * x_offset
            y = r * y_offset
            if r % 2 == 1:
                x += x_offset / 2
            if r < len(text) and c < len(text[r]):
                hexagon(x, y, color='lightblue')
                ax.text(x, y, text[r][c], ha='center', va='center', fontsize=12)

    plt.show()


if __name__ == "__main__":

    # Example usage:
    text_to_display = [
        ['A', 'B', 'C', 'D'],
        ['E', 'F', 'G'],
        ['H', 'I', 'J', 'K', 'L']
    ]

    hex_lattice(text_to_display, size=1, figsize=(10, 8))