This is example implementation of Juliset Class to explore fractals.

# The class has several methods defined. The program used the pandas , numpy and mathplotlib libraries.
# The __init__ method is defining the various variables used in the program
# The juliamap method return the square of the complex number and adds the value c that is passed as a argument to the class
# The iterate metod takes a complex number and iterates upto n times ( default is 100) and returns 0. If the value computed is greater than the range, it returns the value n.
# The create complex plane method creates list of segments with the ranges defined as parameters and the spacing between these segments.
# The set spacing method sets the parameters for the range of the plane and the spacing between the points in the plane.
# The generate method sets the spacing and creates the points in the complex plane by calling the iterate method

In [1]:
class JuliaSet():
    
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    def __init__(self, c, n=100): # This is the data for the class - 
        self.c = c     
        self.n = n
        self._d = 0.001
        self.lowrange = -2
        self.highrange = 2
        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) > self.highrange :
                return(m)
            elif m >= self.n :
                return(0)

            
    def create_complexplane(self,lowrange, highrange, d) :
        list1 = [ ]
        coun = self.np.arange(lowrange,highrange,d)
        list1=[complex(x,y) for x in coun for y in coun]
        return list1 
    

    def set_spacing(self, lowrange, highrange, d) : # This is the method for the class 
        self.lowrange = lowrange
        self.highrange = highrange
        self._d = d
        self._complexplane = self.create_complexplane(self.lowrange,self.highrange,self._d)
        #print self._complexplane  # to troubleshoot the code
   
    def generate(self) :
        self.set_spacing(self.lowrange, self.highrange, self._d)
        for complex_number in self._complexplane:
             self.set.append(self.iterate(complex_number))
        return self.set

In [ ]:
# This code for testing and troubleshooting only
#j = JuliaSet(1+1j)
#print j.create_complexplane(-2,2,0.5)
#j.set_spacing(-4,4,0.2)
#print j._complexplane
#print j.generate()
# The code below uses various graphics libraries to plot the points generated by the generate method of the Juliaset class. 

In [18]:
# 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((self.highrange - self.lowrange) / 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(self.lowrange,self.highrange,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=(self.lowrange,self.highrange), y_range=(self.lowrange,self.highrange), plot_width=600, plot_height=600)
        f.image(image=[self.img], x=[self.lowrange], y=[self.lowrange], dw=[(self.highrange - self.lowrange)], dh=[(self.highrange - self.lowrange)], palette=bokehpalette, dilate=True)
        blt.show(f)
        
j = JuliaSetPlot(4+2j)
#%time j.set_spacing(-2,2,0.006)
#%time j.interact()
#j = JuliaSet(1+1j)
#print j.create_complexplane(-2,2,0.5)
j.set_spacing(-4,4,0.01)
j.interact()


BokehJS successfully loaded.

In [ ]: