In [1]:
import numpy as np

In [5]:
# We are going to fill in an array

# with N squares
N = 50000

# at different slices, starting at:
m = np.random.random_integers(0, 2000, N)
n = np.random.random_integers(0, 2000, N)

# with some random sizes:
size = 2**(np.random.binomial(7, 0.2, N) + 1)

# create an empty array, with masked values
img = np.ma.empty((2128, 2128), dtype='int32')
img.mask = True

# this is a slow version (500ms on my machine)
for i, (m_i, n_i, size) in enumerate(zip(m, n, size)):
    img[m_i:(m_i+size),n_i:(n_i+size)] = i 

# I also tried:
# img[m:(m+size), n:(n+size)] = np.ndindex(m.shape)


1 loops, best of 3: 605 ms per loop

In [9]:
plt.imshow(img, cmap='Accent')


Out[9]:
<matplotlib.image.AxesImage at 0x2a8ad10>

In [ ]: