Skip to content

Instantly share code, notes, and snippets.

@jbn
Created May 13, 2018 15:11
Show Gist options
  • Select an option

  • Save jbn/c4a69346356f633a5d6ac34e54b7ee2b to your computer and use it in GitHub Desktop.

Select an option

Save jbn/c4a69346356f633a5d6ac34e54b7ee2b to your computer and use it in GitHub Desktop.
Pretty Squares
def portray_neighborhood_offsets(C, origin, dist_func=None):
C = C.copy()
C[origin] = 2
fig, ax = plt.subplots(1, 1)
ax.imshow(C,
interpolation='nearest', # Not 'none'!
aspect='equal',
cmap=plt.cm.Purples)
ax.set_xticks(np.arange(-.5, C.shape[1], 1), minor=True)
ax.set_xticklabels([])
ax.set_yticks(np.arange(-.5, C.shape[0], 1), minor=True)
ax.set_yticklabels([])
ax.grid(which='minor', color='w', linestyle='-', linewidth=1)
if dist_func:
for i in range(C.shape[0]):
for j in range(C.shape[1]):
if (i, j) == origin:
d = 0
else:
d = dist_func((0, 0), (i - origin[0], j - origin[1]))
text = ax.text(i, j, d,
ha="center", va="center", color="w")
return fig
r = 4
offsets = von_neumann_neighborhood_offsets(r, False)
C, origin = neighborhood_grid_from_offsets(offsets)
dist_func = von_neumann_neighborhood_offsets.dist_func
portray_neighborhood_offsets(C, origin, dist_func)
msg = "{} Neighborhood ({} Distance)"
plt.gca().set_title(msg.format("Von Neumann",
dist_func.__name__.capitalize()))
replot_as_svg();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment