Open In App

Barnsley Fern in Python

Last Updated : 22 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report
Barnsley fern is a fractal shape created by mathematician Michael Barnsley. The geometric features of this fractal resemble a natural fern and hence it gets its name. Barnsley fern is created by iterating over a large number of times on four mathematical equations, introduced by Barnsley, known as Iterated Function System (IFS). The transformation Barnsley used had the formula : where, the letters had following value :
a b c d e f p PART
0 0 0 0.16 0 0 0.01 Stem
0.85 0.04 -0.04 0.85 0 1.60 0.85 Small Leaflet
0.20 -0.26 0.23 0.22 0 1.60 0.07 Large Leaflet(Left)
-0.15 0.28 0.26 0.24 0 0.44 0.07 Large Leaflet(Right)
and “p” is the probability. Thus the four equations are:   f_1 :\\     x_{n+1} = 0\\     y_{n+1} = 0.16 y_n\\ \\ f_2 :\\     x_{n+1} = 0.85 x_n + 0.04 y_n\\     y_{n+1} = -0.04 x_n + 0.85 y_n + 1.6\\ \\ f_3 :\\     x_{n+1} = 0.2 x_n - 0.26 y_n\\     y_{n+1} = 0.23 x_n + 0.22 y_n + 1.6\\ \\ f_4 :\\     x_{n+1} = -0.15 x_n + 0.28 y_n\\     y_{n+1} = 0.26 x_n + 0.24 y_n + 0.44\\ With the help of above equations, the fern is created. Now lets see the Python3 implementation for the same.
# importing necessary modules
import matplotlib.pyplot as plt
from random import randint
  
# initializing the list
x = []
y = []
  
# setting first element to 0
x.append(0)
y.append(0)
  
current = 0
  
for i in range(1, 50000):
  
    # generates a random integer between 1 and 100
    z = randint(1, 100)
  
    # the x and y coordinates of the equations
    # are appended in the lists respectively.
      
    # for the probability 0.01
    if z == 1:
        x.append(0)
        y.append(0.16*(y[current]))
      
    # for the probability 0.85    
    if z>= 2 and z<= 86:
        x.append(0.85*(x[current]) + 0.04*(y[current]))
        y.append(-0.04*(x[current]) + 0.85*(y[current])+1.6)
      
    # for the probability 0.07    
    if z>= 87 and z<= 93:
        x.append(0.2*(x[current]) - 0.26*(y[current]))
        y.append(0.23*(x[current]) + 0.22*(y[current])+1.6)
      
    # for the probability 0.07    
    if z>= 94 and z<= 100:
        x.append(-0.15*(x[current]) + 0.28*(y[current]))
        y.append(0.26*(x[current]) + 0.24*(y[current])+0.44)
          
    current = current + 1
   
plt.scatter(x, y, s = 0.2, edgecolor ='green')
  
plt.show()        

                    
Output : Note: The whole output depends upon the coefficients of the equations. One experiment might be to change the coefficients and get a new pattern every time.

Similar Reads

Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
JavaScript vs Python : Can Python Overtop JavaScript by 2020?
This is the Clash of the Titans!! And no...I am not talking about the Hollywood movie (don’t bother watching it...it's horrible!). I am talking about JavaScript and Python, two of the most popular programming languages in existence today. JavaScript is currently the most commonly used programming language (and has been for quite some time!) but now
5 min read
Python | Visualizing O(n) using Python
Introduction Algorithm complexity can be a difficult concept to grasp, even presented with compelling mathematical arguments. This article presents a tiny Python program that shows the relative complexity of several typical functions. It can be easily adapted to other functions. Complexity. Why it matters? Computational complexity is a venerable su
3 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
Python | Convert list to Python array
Sometimes while working in Python we can have a problem in which we need to restrict the data elements to just one type. A list can be heterogeneous, can have data of multiple data types and it is sometimes undesirable. There is a need to convert this to a data structure that restricts the type of data. Convert List to Array PythonBelow are the met
3 min read
Article Tags :
Practice Tags :