The Barnsley Fern: Mathematical Art

This pattern of a fern pictured above was generated by a simple iterative program designed by mathematician Michael Barnsely.  I downloaded the Python code from the excellent Tutorialspoint and then modified it slightly to run on  What we are seeing is the result of 40,000 individual points – each plotted according to a simple algorithm.  The algorithm is as follows:

Transformation 1: (0.85 probability of occurrence)

xi+1 = 0.85xi +0.04yi

yi+1= -0.04xi+0.85yi+1.6

Transformation 2: (0.07 probability of occurrence)

xi+1 = 0.2xi -0.26yi

yi+1= 0.23xi+0.22yi+1.6

Transformation 3: (0.07 probability of occurrence)

xi+1 = -0.15xi -0.28yi

yi+1= 0.26xi+0.24yi+0.44

Transformation 4: (0.01 probability of occurrence)

xi+1 = 0

yi+1= 0.16yi

So, I start with (0,0) and then use a random number generator to decide which transformation to use.  I can run a generator from 1-100 and assign 1-85 for transformation 1, 86-92 to transformation 2, 93-99 for transformation 3 and 100 for transformation 4.  Say I generate the number 36 – therefore I will apply transformation 1.

xi+1 = 0.85(0)+0.04(0)

yi+1= -0.04(0)+0.85(0)+1.6

and my new coordinate is (0,1.6).  I mark this on my graph.

I then repeat this process – say this time I generate the number 90.  This tells me to do transformation 2.  So:

xi+1 = 0.2(0) -0.26(1.6)

yi+1= 0.23(0)+0.22(1.6)+1.6

and my new coordinate is (-0.416, 1.952).  I mark this on my graph and carry on again.  The graph above was generated with 40,000 iterations – let’s see how it develops over time:

1000 iterations:

10,000 iterations:

100,000 iterations:


500,000 iterations:

If we want to understand what is happening here we can think of each transformation as responsible for a different part of our fern.  Transformation 1 is most likely and therefore this fills in the smaller leaflets. Transformations 2 and 3 fill in the bottom left and right leaflet (respectively) and transformation 4 fills in the stem.

It’s quite amazing to think that a simple computer program can create what looks like art – or indeed that is can replicate what we see in nature so well.  This fern is an example of a self-similar pattern – i.e one which will look the same at different scales.  You could zoom into a detailed picture and see the same patterns repeating.  You might want to explore the idea of fractals in delving into this topic in more detail.

Changing the iterations

We can explore what happens when we change the iterations very slightly.

Christmas tree

Crazy spiral

Modern art


You can modify the code to run this here.  Have a go!