In [41]:
class JuliaSet():
import numpy as np
def __init__(self, c, n=100): # This is the data for the class -
self.c = c
self.n = n
self._d = 0.001
self.set = [ ]
self._complexplane = [ ]
def juliamap(self, z) : # This is the method for the class
return (z**2 + self.c)
def iterate(self, z): # This is the method for the class
m = 0
while True:
z = self.juliamap(z)
m = m+1
if abs(z) > 2 :
return(m)
elif m >= self.n :
return(0)
def create_complexplane(self,d) :
list1 = [ ]
coun = np.arange(-2,2,d)
list1=[complex(x,y) for x in coun for y in coun]
return list1
def set_spacing(self, d) : # This is the method for the class
self._d = d
self._complexplane = self.create_complexplane(self._d)
#print self._complexplane # to troubleshoot the code
def generate(self) :
self.set_spacing(self._d)
for complex_number in self._complexplane:
self.set.append(self.iterate(complex_number))
return self.set
In [46]:
j = JuliaSet(0)
j.set_spacing(0.5)
print j._complexplane
print j.generate()
In [44]:
# from juliasets2 import JuliaSet
# Math libraries
import numpy as np
from math import sqrt
# Matplotlib plotting libraries
import matplotlib.pyplot as plt
%matplotlib inline
# Bokeh plotting libraries
import bokeh.plotting as blt
blt.output_notebook()
class JuliaSetPlot(JuliaSet):
"""Extend JuliaSet to add plotting functionality"""
def __init__(self, *args, **kwargs):
# Invoke constructor for JuliaSet first, unaltered
JuliaSet.__init__(self, *args, **kwargs)
# Add another attribute: a rendered image array
self.img = np.array([])
def get_dim(self):
"""Return linear number of points in axis"""
return int(4.0 / self._d)
def render(self):
"""Render image as square array of ints"""
if not self.set: self.generate()
# Convert inefficient list to efficient numpy array
self.img = np.array(self.set)
# Reshape array into a 2d complex plane
dim = int(sqrt(len(self.img)))
self.img = np.reshape(self.img, (dim,dim)).T
def show(self):
"""Use matplotlib to plot image as an efficient mesh"""
if not self.img.size: self.render()
plt.figure(1, figsize=(12,9))
dim = int(sqrt(len(self.img)))
xy = np.linspace(-2,2,dim)
plt.pcolormesh(xy, xy, self.img, cmap=plt.cm.hot)
plt.colorbar()
plt.show()
def interact(self):
"""Use bokeh to plot an interactive image"""
from matplotlib.colors import rgb2hex
if not self.img.size: self.render()
# Mimic matplotlib "hot" color palette
colormap = plt.cm.get_cmap("hot")
bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]
# Create bokeh figure
f = blt.figure(x_range=(-2,2), y_range=(-2,2), plot_width=600, plot_height=600)
f.image(image=[self.img], x=[-2], y=[-2], dw=[4], dh=[4], palette=bokehpalette, dilate=True)
blt.show(f)
j = JuliaSetPlot(0.3)
%time j.set_spacing(0.006)
%time j.interact()
In [ ]: