Spaces:
Runtime error
Runtime error
import matplotlib.pyplot as plt | |
import numpy as np | |
def draw_hexagonal_grid(rows, cols, text): | |
fig, ax = plt.subplots() | |
ax.set_aspect('equal') | |
# Create a hexagonal lattice | |
for i in range(rows): | |
for j in range(cols): | |
if i % 2 == 0: | |
y = i * np.sqrt(3) / 2 | |
x = j * 3 / 2 | |
else: | |
y = (i + 0.5) * np.sqrt(3) / 2 | |
x = (j + 0.5) * 3 / 2 | |
ax.add_patch(plt.RegularPolygon((x, y), numVertices=6, radius=0.5, orientation=np.pi/6, fill=None)) | |
# Add text inside each cell | |
ax.text(x, y, text[i * cols + j], ha='center', va='center') | |
ax.autoscale_view() | |
ax.axis('off') | |
plt.show() | |
# Example usage | |
text = "HELLO" | |
rows = 5 | |
cols = 5 | |
draw_hexagonal_grid(rows, cols, text) | |