如何正确绘制具有偏移坐标的六边形?

Але*_*нко 3 python matplotlib hexagonal-tiles coordinate-systems

这是我的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np


offCoord = [[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2]]

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

for c in offCoord:
    hex = RegularPolygon((c[0], c[1]), numVertices=6, radius=2./3., alpha=0.2, edgecolor='k')
    ax.add_patch(hex)
plt.autoscale(enable = True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

附加图像中的预期结果与实际结果

预期结果与实际结果

请告诉我为什么我的六边形不是一条边对齐而是相互重叠?我究竟做错了什么?

Ana*_*y R 5

使用余弦定律(对于角为 120 度且边为 r、r 和 1 的等腰三角形):

1 = r*r + r*r - 2*r*r*cos(2pi/3) = r*r + r*r + r*r = 3*r*r

r = sqrt(1/3)
Run Code Online (Sandbox Code Playgroud)

三角形

这是正确的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np


offCoord = [[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2]]

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

for c in offCoord:
    # fix radius here
    hexagon = RegularPolygon((c[0], c[1]), numVertices=6, radius=np.sqrt(1/3), alpha=0.2, edgecolor='k')
    ax.add_patch(hexagon)
plt.autoscale(enable = True)
plt.show()
Run Code Online (Sandbox Code Playgroud)