Created
July 17, 2017 19:43
-
-
Save joelbecker/401249a0a1c1fc8f9d3275c65f25e924 to your computer and use it in GitHub Desktop.
This is a supplementary script for NetLab's tutorial on candidate link indexing with the recordlinkage data integration library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Adapted from https://stackoverflow.com/questions/19586828/drawing-grid-pattern-in-matplotlib | |
""" | |
import matplotlib | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
N = 10 | |
# make base data sets | |
data_1 = np.ones((N, N//2)) * 2 | |
data_2 = np.ones((N, N//2)) * np.nan | |
# fill in some fake data_2 | |
r1 = range(0, 4) | |
for i in r1: | |
for j in range(0,2): | |
data_2[i, j] = 1 | |
r2 = range(4, 6) | |
for i in r2: | |
for j in range(2, 3): | |
data_2[i, j] = 2 | |
r3 = range(6, 10) | |
for i in r3: | |
for j in range(3, 5): | |
data_2[i, j] = 3 | |
# make a figure + axes | |
fig, ax = plt.subplots(1, 1, tight_layout=True) | |
# make color map | |
my_cmap = matplotlib.colors.ListedColormap([plt.get_cmap('tab20c').colors[6], | |
plt.get_cmap('tab20c').colors[2], | |
plt.get_cmap('tab20c').colors[14]]) | |
# set the 'bad' values (nan) to be white and transparent | |
my_cmap.set_bad(color='w', alpha=0) | |
# draw the grid | |
for x in range(N + 1): | |
ax.axhline(x, lw=2, color='w', zorder=5) | |
ax.axvline(x, lw=2, color='w', zorder=5) | |
# Which version do you want to draw? | |
# Set draw = 1 for "full index" visualization | |
# Set draw = 2 for "blocked index" visualization | |
draw = 1 | |
# draw the boxes | |
if draw is 1: | |
ax.imshow(data_1, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N//2], zorder=0) | |
ax.axis('off') | |
plt.savefig('block_index_1.png', transparent=True) | |
if draw is 2: | |
ax.imshow(data_2, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N//2], zorder=0) | |
ax.axis('off') | |
plt.savefig('block_index_2.png', transparent=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment