Last active
September 19, 2019 23:21
-
-
Save audreyfeldroy/0c5d0923501c08a7cb7b13e08c66a7ad to your computer and use it in GitHub Desktop.
A snippet for https://www.codemakesmehappy.com/
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
""" | |
Based on CarlosFocil's mandalapy code: https://github.com/CarlosFocil/mandalapy | |
Which is an adaptation of Fronkonstin's R code: https://github.com/aschinchon/mandalas | |
""" | |
from scipy.spatial import Voronoi, voronoi_plot_2d | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
from math import pi | |
# Modify the following parameters in order to get different figures | |
# Number of iterations (how many times the equidistant points will be generated) | |
it = 3 | |
points = 6 # Number of points to draw | |
radius = 4 # Factor of expansion or compression | |
f1, f2 = 10, 10 # Figure size | |
def MandalaPy(it, points, radius): | |
""" MandalaPy creates mandalas. """ | |
angles = np.linspace(0, 2*pi*(1-1/points), points) + pi/2 | |
x, y = 0, 0 | |
df = pd.DataFrame([[x, y]], columns=['x', 'y']) # Initial center | |
# Iterate over centers | |
for k in range(it): | |
t1 = np.array([]) | |
t2 = np.array([]) | |
for i in range(df.shape[0]): | |
t1 = np.append(t1, df['x'][i]+radius**(k)*np.cos(angles)) | |
t2 = np.append(t2, df['y'][i]+radius**(k)*np.sin(angles)) | |
df = pd.DataFrame(np.column_stack((t1, t2)), columns=['x', 'y']) | |
mandala = Voronoi(df) | |
fig_size = plt.rcParams["figure.figsize"] | |
fig_size[0], fig_size[1] = f1, f2 | |
voronoi_plot_2d( | |
vor=mandala, | |
show_points=True, | |
show_vertices=True, | |
), plt.axis('off'), plt.gca().set_aspect('equal', adjustable='box'), plt.savefig('mandala.png', dpi=100) | |
return plt.show() | |
print(MandalaPy(it, points, radius)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment