I tried drawing a pseudo fractal figure using Python

Today, I decided to draw a figure written in graffiti with a program (matplotlib), so I drew it using Python.

Program to be used this time

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.

After execution

fractal.png

Oh, beautiful (likely on a CD jacket) (By the way, if you have any name, please let me know in the comment section)

[Addition]

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".

Referenced articles

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

I tried drawing a pseudo fractal figure using Python
I tried drawing a line using turtle
I tried reading a CSV file using Python
[Python] I tried using OpenPose
I tried to implement a pseudo pachislot in Python
I tried using Python (3) instead of a scientific calculator
I made a Line-bot using Python!
I tried using Thonny (Python / IDE)
[Python] I tried using YOLO v3
I tried to make a stopwatch using tkinter in python
I tried to make a regular expression of "amount" using Python
I tried playing a ○ ✕ game using TensorFlow
I tried to make a regular expression of "time" using Python
I tried using Bayesian Optimization in Python
I tried to make a regular expression of "date" using Python
I tried using UnityCloudBuild API from Python
I tried to make a todo application using bottle with python
I tried using pipenv, so a memo
I tried to create a sample to access Salesforce using Python and Bottle
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried playing a typing game in Python
I tried using mecab with python2.7, ruby2.3, php7
[Memo] I tried a pivot table in Python
I tried using Pythonect, a dataflow programming language.
I tried using the Datetime module by Python
I tried adding a Python3 module in C
I tried running alembic, a Python migration tool
I tried using a database (sqlite3) with kivy
I tried to make a ○ ✕ game using TensorFlow
I tried using argparse
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried Python> decorator
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Python] I immediately tried using Pylance's VS Code extension.
I made a login / logout process using Python Bottle.
I tried to draw a route map with Python
I tried running python etc. from a bat file
I tried hosting a Pytorch sample model using TorchServe
I tried to automatically generate a password with Python3
I tried reading data from a file using Node.js.
Python: I tried a liar and an honest tribe