""" """ from random import random import matplotlib.pyplot as plt import numpy as np def generate_grid(proportion, n, m=0): """Generate a grid nxm with random 1 (with proportion) or 0""" if m == 0: m = n return [[int(random() < proportion) for _ in range(m)] for _ in range(n)] def trim_axs(axs, N): """ Reduce *axs* to *N* Axes. All further Axes are removed from the figure. """ axs = axs.flat for ax in axs[N:]: ax.remove() return axs[:N] def draw_image(grid, ax): hight = len(grid) lenght = len(grid[0]) ax.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False) ax.imshow(grid, cmap="binary") ax.hlines( y=np.arange(0, hight) + 0.5, xmin=np.full(lenght, 0) - 0.5, xmax=np.full(lenght, hight) - 0.5, color="gray", ) ax.vlines( x=np.arange(0, lenght) + 0.5, ymin=np.full(lenght, 0) - 0.5, ymax=np.full(lenght, hight) - 0.5, color="gray", ) return ax def grid_to_image(grids, filename): """ """ plt.clf() n_cols = 3 n_rows = len(grids) // 3 axs = plt.figure(constrained_layout=True).subplots(n_rows, n_cols) axs = trim_axs(axs, len(grids)) for ax, grid in zip(axs, grids): draw_image(grid, ax) plt.savefig(filename) if __name__ == "__main__": specs = [ {"proportion": 0.2, "n": 5}, {"proportion": 0.3, "n": 5}, {"proportion": 0.2, "n": 8}, {"proportion": 0.3, "n": 8}, {"proportion": 0.2, "n": 10}, {"proportion": 0.3, "n": 10}, ] grids = [generate_grid(**spec) for spec in specs] grid_to_image(grids, "grids.pdf")