Dienstag, 29. Juli 2014

Matplotlib Animation in Ipython - Chaos Game: Sierpinski Triangle

Sierpinski_Animation

Chaos and Fractals

Sierpinski Triangle with a Chaos Game using Interactive Matplotlib

I am new to Fractals but i really love the idea of nature doing things we do not see as structured objects, but that indeed are.

Building figures like the following one often implies using feedback systems. You can think of a function that is executed on its own output for many iterations. The most common known figure is the Mandelbrot set.

Here we find a Fractal with the "Hipster-Object" the Triangle

In []:
 

i had issues finding the right preparation for an interactive plot updating on each iteration: i found a solution here for ipython: http://stackoverflow.com/questions/21360361/how-to-dynamically-update-a-plot-in-a-loop-in-ipython-notebook-within-one-cell

In [1]:
# the marked #! lines are essential for the interactivity
%matplotlib inline #!
import random
import numpy as np
import time
import pylab as pl #!
from IPython import display #!
import numpy as np
In [14]:
pylab.rcParams['figure.figsize'] = (10.0, 8.0)
In [34]:
def Sierpinski_Chaos(gp=None,n=None):
    """ This Chaos game shows how randomness can lead to structure:
    1. 3 points have to be drawn in the plane
    2. Mark a triangle with these points with pl.plot
    3. draw a random point outside this triangle- this is the first 'Game-Point(gp)'
    Repeat til n:
    4. Choose a randomly a base-point out of the three corner points 
    5. Build the vector between the 'Game-Point' and the randomly chosen base-point
    and mark a point (scatter) half way to the base-point
    Created 08-29-2014 by Pierre Noire """ 
    
    #1.,2.
    pl.plot([0,4,2,0],[0,0,4,0])
    pl.xlim(0,4)
    pl.ylim(0,4)
    base_points=[[0,0],[4,0],[2,4]]
    if gp==None:
        gp=np.array([5,5])#starting game_point
    if n==None:
        n=500 #number of iterations
    for n in range(500):
        gp_log=gp.copy()
        pl.scatter(gp[0],gp[1],lw='0',s=20)#3.
        pl.xlim(0,4)#!
        pl.ylim(0,4)#!
        pl.draw()#!
        display.clear_output(wait=True)#!
        display.display(pl.gcf())#!
        time.sleep(0.0000005)#!
        #4
        fort_wheel=random.choice(base_points)
        rand_base=np.array(fort_wheel)
        #5
        gp=gp-1.0/2*(gp-rand_base)
        gp_log=np.concatenate((gp_log,gp))
        #(gp-rand_base) is 
        #"direction-vector" starting from the gp and just walking half way leads to new gp    
    return gp_log

To Read more about this Chaos Game look here:

In [35]:
build_hipster=Sierpinski_Chaos(n=1500)
In [23]:
from IPython.display import IFrame
IFrame('https://en.wikipedia.org/wiki/Chaos_game', width=700, height=500)
Out[23]:
In [38]:
#%install_ext http://raw.github.com/jrjohansson/version_information/master/version_information.py #install if you haven´t yet
%load_ext version_information
%reload_ext version_information

%version_information numpy, matplotlib,
The version_information extension is already loaded. To reload it, use:
  %reload_ext version_information

Out[38]:
SoftwareVersion
Python2.7.6 |Anaconda 1.9.1 (64-bit)| (default, Jan 17 2014, 10:13:17) [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]
IPython2.0.0-wakari
OSposix [linux2]
numpy1.8.1
matplotlib1.3.1
Wed Jul 30 01:24:46 2014 MSD
In []: