Plotting arrays with flat

In this notebook we’ll have data in the form of numpy arrays and visualize them through shapes in flat.

Imports

from flat import document, shape, rgb
from IPython.display import display, SVG
import numpy as np

Rectangles

Every item in the numpy array is visualized through a rectangle. If the number is 1, the fill color of the rectangle is black, otherwise it’s white.

# Create an array with 0 and 1s
data = np.random.randint(low=0, high=2, size=(4,4), dtype=int)
print(data)

# Document setup

size = 100
d = document(size, size, 'mm')
black = rgb(0, 0, 0)
white = rgb(255, 255, 255)

p = d.addpage()

grid_n, _ = data.shape
cell_size = size//grid_n
it = np.nditer(data.T, flags=['multi_index'])

for item in it:
    x, y = it.multi_index
    val = int(item)
    s = shape()
    if val == 1:
        # s.fill(black).stroke(black) # Stroke in the same color = no grid
        s.fill(black).stroke(rgb(200, 200, 200)) # With a different stroke the grid becomes visible
        rect = s.rectangle(x*cell_size, y*cell_size, cell_size, cell_size)
    else:
        # s.fill(white).stroke(white) # Stroke in the same color = no grid
        s.fill(white).stroke(rgb(200, 200, 200)) # With a different stroke the grid becomes visible
        rect = s.rectangle(x*cell_size, y*cell_size, cell_size, cell_size)
    p.place(rect)
    
# draw_grid(p, grid=(grid_n+1, grid_n+1), size=(size, size))
    
display(SVG(p.svg()))
[[0 0 0 1]
 [0 1 1 0]
 [0 1 0 1]
 [1 1 1 0]]
_images/plotting_arrays_flat_4_1.svg

Circles

Same as above but with circles insted of rectangles.

data = np.random.randint(low=0, high=2, size=(4,4), dtype=int)
print(data)

size = 100
d = document(size, size, 'mm')
black = rgb(0, 0, 0)
white = rgb(255, 255, 255)

p = d.addpage()

grid_n, _ = data.shape 
cell_size = size//grid_n

it = np.nditer(data.T, flags=['multi_index'])

for item in it:
    x, y = it.multi_index
    val = int(item)
    s = shape()
    # fill cell if val == 1
    if val == 1:
        s.fill(black).nostroke()
        form = s.circle(x*cell_size+(cell_size//2), y*cell_size+(cell_size//2), cell_size//2)
    else:
        s.fill(white).stroke(white)
        form = s.rectangle(x*cell_size, y*cell_size, cell_size, cell_size)
        # form = s.circle(x*cell_size+(cell_size//2), y*cell_size+(cell_size//2), cell_size//2+2)
    p.place(form)

    
display(SVG(p.svg()))
[[0 0 0 1]
 [0 1 0 1]
 [1 0 1 0]
 [1 0 0 1]]
_images/plotting_arrays_flat_6_1.svg

Circles of different sizes

steps = 6
data = np.random.randint(low=1, high=steps+1, size=(10,10)) / steps
# print(data)

size = 100
d = document(size, size, 'mm')
black = rgb(0, 0, 0)
white = rgb(255, 255, 255)

p = d.addpage()

grid_n, _ = data.shape 
cell_size = size//grid_n

it = np.nditer(data.T, flags=['multi_index'])

for item in it:
    x, y = it.multi_index
    s = shape()

    s.fill(black).nostroke()
    form = s.circle(x*cell_size+(cell_size//2), y*cell_size+(cell_size//2), cell_size//2*item)

    p.place(form)

    
display(SVG(p.svg()))
_images/plotting_arrays_flat_8_0.svg

Appendix

Iterate over a numpy array with index

Iterate over a numpy array and keep track of the index.

See https://numpy.org/doc/stable/reference/arrays.nditer.html#tracking-an-index-or-multi-index

a = np.arange(6).reshape(2,3)
print(a)

# create an iterator
# the array is transposed to transfer from
# (y, x) (numpy) to (x, y) (flat)
it = np.nditer(a.T, flags=['multi_index'])

for item in it:
    x, y = it.multi_index
    val = int(item)
    
    print(f"cell {x}/{y}: {val}")
[[0 1 2]
 [3 4 5]]
cell 0/0: 0
cell 1/0: 1
cell 2/0: 2
cell 0/1: 3
cell 1/1: 4
cell 2/1: 5