Creating an hexagonal grid
A quick note as to how to create an hexagonal grid.
Let's first initialize the notebook:
In [1]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
phi = (np.sqrt(5)+1)/2
fig_width = 21
%load_ext autoreload
%autoreload 2
In [2]:
import numpy as np
np.meshgrid?
first a rectangular grid¶
In [3]:
N = 121
N_X = int(np.sqrt(N))
N_Y = N // N_X
xv, yv = np.meshgrid(np.arange(N_X), np.arange(N_Y), sparse=False, indexing='xy')
xv, yv
Out[3]:
In [4]:
fig, ax = plt.subplots(figsize=(fig_width, fig_width))
ax.scatter(xv, yv)
Out[4]:
second an hexagonal grid¶
In [5]:
ratio = np.sqrt(3)/2 # cos(60°)
ratio
Out[5]:
In [6]:
N_X = int(np.sqrt(N)/ratio)
N_Y = N // N_X
xv, yv = np.meshgrid(np.arange(N_X), np.arange(N_Y), sparse=False, indexing='xy')
In [7]:
fig, ax = plt.subplots(figsize=(fig_width, fig_width))
ax.scatter(xv, yv)
Out[7]:
In [8]:
xv = xv * ratio
xv, yv
Out[8]:
In [9]:
fig, ax = plt.subplots(figsize=(fig_width, fig_width))
ax.scatter(xv, yv)
Out[9]:
intercalate every two lines by half the width
In [10]:
xv[::2, :] += ratio/2
In [11]:
fig, ax = plt.subplots(figsize=(fig_width, fig_width))
ax.scatter(xv, yv)
Out[11]:
A bigger one for the show:
In [12]:
N = 2345
ratio = np.sqrt(3)/2 # cos(60°)
N_X = int(np.sqrt(N/ratio))
N_Y = N // N_X
xv, yv = np.meshgrid(np.arange(N_X), np.arange(N_Y), sparse=False, indexing='xy')
xv = xv * ratio
xv[::2, :] += ratio/2
In [13]:
fig, ax = plt.subplots(figsize=(fig_width, fig_width))
ax.scatter(xv, yv, s=1)
ax.axis('equal');
some book keeping for the notebook¶
In [14]:
%load_ext watermark
%watermark -i -h -m -v -p numpy,matplotlib -r -g -b