-
-
Save dinhanhthi/1802f242347c8546deddd568035c6221 to your computer and use it in GitHub Desktop.
Plot a grid of squares
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
N = 100 | |
# Plot a square of NxN small other squares. | |
# Each one has a probability Pxi/N of being coloured, where i is the line where it stands. | |
P = 0.5 | |
im_size = 5 | |
image = np.repeat(np.array(range(1,N+1)).reshape(N, 1), N, axis=1) | |
# LESS understandable but executing FASTER | |
image = (P/N * image) <= np.random.rand(N,N) | |
# MORE understandable but executing SLOWER | |
def bernoulli(num, P, N): | |
return 1-np.random.binomial(1, P*num/N) | |
vfunc = np.vectorize(bernoulli) | |
image = vfunc(image, P, N) | |
plt.figure(figsize=(im_size, im_size)) | |
plt.imshow(image, cmap='gray') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment