在 matplotlib 中映射一个六边形网格
map a hexagonal grid in matplotlib
我想画一个六边形网格的图形。最终结果应该看起来像一个蜂窝。但是,我无法使用 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()
在 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)
我想画一个六边形网格的图形。最终结果应该看起来像一个蜂窝。但是,我无法使用 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()
在 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)