【发布时间】: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