Skip to content

Instantly share code, notes, and snippets.

@scjody
Created May 12, 2013
Embed
What would you like to do?
Script to plot a Barnsley fern using plot.ly. I feel this pushes the boundaries of what the site was made to do, but it works and I like it :)
# Python 2.7.3 (default, Apr 20 2012, 23:04:22) [GCC 4.6.3]
# Use plot() to plot lists or arrays
# Use print to show results in the command line history
from numpy import matrix
import random
# Define probabilities, multiplication matrices, and transforms
p1 = 0.01
m1 = matrix('0.00, 0.00; 0.00, 0.16')
t1 = matrix('0.00; 0.00')
p2 = 0.85
m2 = matrix('0.85, 0.04; -0.04, 0.85')
t2 = matrix('0.00; 1.60')
p3 = 0.07
m3 = matrix('0.20, -0.26; 0.23, 0.22')
t3 = matrix('0.00; 1.60')
p4 = 0.07
m4 = matrix('-0.15, 0.28; 0.26, 0.24')
t4 = matrix('0.00; 0.44')
x = []
y = []
c = matrix('0; 0')
random.seed()
for i in xrange(20000):
r = random.random()
if r < p1:
c = m1 * c + t1
elif r < (p1 + p2):
c = m2 * c + t2
elif r < (p1 + p2 + p3):
c = m3 * c + t3
else:
c = m4 * c + t4
x.append(c[0, 0])
y.append(c[1, 0])
plot(x, y, {'color': 'green'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment