Today, I decided to draw a figure written in graffiti with a program (matplotlib), so I drew it using Python.
fractal.py
import matplotlib.pyplot as plt
import matplotlib.collections as mc
import numpy as np
import math
def return_point(p1, p2):
point = (p2-p1)*0.08 + p1
return point
N = 1000
x = np.linspace(-1, 1, N)
x_point = [0, 100, 200]
y_point = [0, math.sqrt(3)*100, 0]
for i in range (N):
new_x_point = return_point(x_point[i], x_point[i+1])
new_y_point = return_point(y_point[i], y_point[i+1])
x_point.append(new_x_point)
y_point.append(new_y_point)
fractal = [[(x_point[i], y_point[i]), (x_point[i+1], y_point[i+1])] for i in range(N)]
lc = mc.LineCollection(fractal, colors='#333333', linewidths=1, antialiased=True)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1)
#↓ Non-essential settings
ax.set_axis_bgcolor('#f3f3f3')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
plt.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)
#↑ Non-essential settings
ax.add_collection(lc)
ax.autoscale()
plt.savefig('./fractal.png')
It is also fun to change the value of 0.08
in the 7th line point = (p2-p1) * 0.08 + p1
.
Oh, beautiful (likely on a CD jacket) (By the way, if you have any name, please let me know in the comment section)
Strictly speaking, I thought it was not a fractal figure, so I changed the title from "I tried to draw a fractal figure using Python" to "I tried to draw a pseudo fractal figure using Python".
Thank you very much.
** Create fractal shapes with python part1 (Sierpinski Gasket) ** https://qiita.com/okakatsuo/items/f2e79fc501ed9f799734
** How to draw multiple line segments with Matplotlib ** https://omedstu.jimdofree.com/2019/10/04/matplotlibで複数の線集合を描画する方法/
** A very summary of matplotlib ** https://qiita.com/nkay/items/d1eb91e33b9d6469ef51#2-グラフaxesの作成
Recommended Posts