python - Matplotlib 补丁 - 圆看起来是椭圆

标签 python matplotlib

我正在尝试创建并绘制六边形网格。为此,我定义了以下函数:

def create_hexagon(center):
        INRADIUS = 0.5
        CIRCUMRADIUS = INRADIUS / math.cos(math.radians(30))
        pt1 = [center[0], center[1] + CIRCUMRADIUS]
        pt4 = [center[0], center[1] - CIRCUMRADIUS]
        pt2 = [center[0] + INRADIUS, center[1]+CIRCUMRADIUS/2]
        pt6 = [center[0] - INRADIUS, center[1]+CIRCUMRADIUS/2]
        pt3 = [center[0] + INRADIUS, center[1]-CIRCUMRADIUS/2]
        pt5 = [center[0] - INRADIUS, center[1]-CIRCUMRADIUS/2]
        points = [pt1, pt2, pt3, pt4, pt5, pt6]
        return points

我知道,这里发生了很多几何图形,但我很确定这 6 个点根据中心点被正确描述。如果您能想到比单独描述这 6 点中的每一点更好的方法,请告诉我! 接下来我要做的是,循环遍历包含所有中心点的列表。然后,我继续在每个中心周围绘制一个六边形:

for pt in centers:
    points = create_hexagon(pt)
    hexagon = plt.Polygon(points, fill=None, edgecolor='k')
    plt.gca().add_patch(hexagon)
    circle = plt.Circle(pt, 0.5 / math.cos(math.radians(30)), fill=None)
    plt.gca().add_patch(circle)

由于我的绘图似乎不正确,所以我也在每个中心周围画了一个圆圈。而且这个圆看起来并不是一个圆,而是一个椭圆: enter image description here 放大后更明显: enter image description here

为什么我没有得到圈子?

最佳答案

你的计算没问题,唯一不好的是比例(x轴没有像y轴一样分级)。

只需添加以下内容:

plt.xlim(0, 4.5)
plt.ylim(0, 3.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()

关于python - Matplotlib 补丁 - 圆看起来是椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60448644/

相关文章:

python - 高效的最近邻搜索特定任务?

python - 有时为参数而不是参数对象传递复杂的字典是否更好?

python - matplotlib:如何使用标记大小/颜色作为绘图中的额外维度?

python - 如何设置图例标记大小和 alpha?

python - 在 python 中使用 kmeans sklearn 聚类数据点

python - 如何从字典有效构建数据框(pandas)

Gmail/主题的 python 电子邮件模块

python - 在单行代码中对整数和字符串进行排序

Python - 如何将 'boundary edge' 绘制到 2D 绘图上

python - 绘制散点图最密集区域的等高线