| import numpy as np |
| import matplotlib.pyplot as plt |
| from matplotlib.patches import RegularPolygon |
|
|
|
|
| |
| def hex_lattice(text, size=1, figsize=(8, 8)): |
| fig, ax = plt.subplots(figsize=figsize) |
| ax.set_aspect('equal') |
| ax.axis('off') |
|
|
| |
| radius = size * np.sqrt(3) / 2 |
| x_offset = size * 1.5 |
| y_offset = size * np.sqrt(3) |
|
|
| |
| 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) |
|
|
| |
| 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__": |
|
|
| |
| 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)) |