【问题标题】:map a hexagonal grid in matplotlib在 matplotlib 中映射六边形网格
【发布时间】:2015-02-22 22:47:00
【问题描述】:

我想画一个带有六边形网格的图形。最终结果应该看起来像一个蜂窝。但是,我无法使用 matplotlib.collections.RegularPolyCollection 正确调整六边形的大小。谁能看到我做错了什么,或提供另一种解决方案。我想这已经完成了,所以我不需要重新发明轮子。

import matplotlib.pyplot as plt
from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
import numpy as np

# Make some offsets, doing 4 polygons for simplicity here
xyo = [(0,0), (1,0), (0,1), (1,1)]
# length of hexagon side
hexside = 1
# area of circle circumscribing the hexagon
circ_area = np.pi * hexside ** 2

fig, ax = plt.subplots(1,1)
col = collections.RegularPolyCollection(6, np.radians(90), sizes = (circ_area,),
    offsets=xyo,transOffset=ax.transData)
ax.add_collection(col, autolim=True)
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c')]
col.set_color(colors)
ax.autoscale_view()
plt.show()

【问题讨论】:

  • 对于它的价值,我认为您的困惑是因为sizes 指定的大小/区域以点为单位,而不是数据坐标。但是,更简单的方法可能是利用 hexbin

标签: python matplotlib grid


【解决方案1】:

谁在 2020+ 年遇到同样的问题,请查看我的 hexalattice 模块: 它允许在 2D 中创建六边形网格(六边形格子),并对六边形的空间分布、格子的圆形裁剪和围绕中心槽的旋转进行精细控制。

用法和图形输出:

from hexalattice.hexalattice import *
hex_centers, _ = create_hex_grid(nx=10,
                                 ny=10,
                                 do_plot=True)
plt.show()    # import matplotlib.pyplot as plt

安装:

'>> pip install hexalattice'

高级功能

该模块允许堆叠少量网格、围绕其中心任意旋转网格、对六边形之间的间隙进行高级控制等。

例子:

hex_grid1, h_ax = create_hex_grid(nx=50,
                              ny=50,
                              rotate_deg=0,
                              min_diam=1,
                              crop_circ=20,
                              do_plot=True)
create_hex_grid(nx=50,
                ny=50,
                min_diam=1,
                rotate_deg=5,
                crop_circ=20,
                do_plot=True,
                h_ax=h_ax)

【讨论】:

  • 有什么简单的方法可以按某种顺序获取顶点坐标吗? (现在只能提取六边形中心)。
  • hexalattice 模块不提供有关顶点的信息,但这些信息的计算很简单:假设六边形位于“尖顶”位置,则可以获得上顶点的位置通过将最大半径添加到中心点。有关更多信息,请参阅这个很棒的博客redblobgames.com/grids/hexagons
猜你喜欢
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
相关资源
最近更新 更多