The modified julia set code below is impleted using numpy. The use of meshgrid funtion improved the effeciency of the code and now plotting takes much lesser time.


In [8]:
# 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()


BokehJS successfully loaded.

In [9]:
import numpy as np

class JuliaSet(object):
    
    def __init__(self, c, n=100):
        self.c = c
        self.n = n
        self._d = 0.001
        self.complexplane = np.array([])
        self.set = np.array([])

        
    def juliamap(self, z):

        return (z**2) + self.c

    

    def iterate(self, z):

        m = 0

        while True:

            z=self.juliamap(z)

            m = m + 1

            if  abs(z) > 2:

                return m

            if m >=self.n:

                return 0



    def set_spacing(self, d,start=-2,final=2):
        self.start=start
        self.final=final
        self._d = d
        i=np.arange(start,final,self._d)
        x,y=np.meshgrid(i,i)
        self._complexplane=x+y*1j


    def generate(self):
        i=np.vectorize(self.iterate)
        self.set = i(self._complexplane)

        return self.set

In [10]:
class JuliaSetPlot(JuliaSet):
    """Extend JuliaSet to add plotting functionality"""
    
    def __init__(self, *args, **kwargs):
        # Invoke constructor for JuliaSet first, unaltered
        super(JuliaSetPlot, self).__init__(*args, **kwargs)
        # Add one more attribute: a rendered image array
    
    def get_dim(self):
        # get what should be an attribute
        return int(sqrt(self.set.size)) #int(4.0 / self._d)
    
        
    def show(self):
        if not self.set.size: self.generate()
        # Specify complex plane axes efficiently
        xy = np.linspace(-2,2,self.get_dim())
        # Use matplotlib to plot image as an efficient mesh
        plt.figure(1, figsize=(12,9))
        plt.pcolormesh(xy,xy,self.set, cmap=plt.cm.hot)
        plt.colorbar()
        plt.show()
        
    def interact(self):
        from matplotlib.colors import rgb2hex
        if not self.set.size: self.generate()
        # Mimic matplotlib "hot" color palette
        colormap = plt.cm.get_cmap("hot")
        bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]
        # Use bokeh to plot an interactive image
        f = blt.figure(x_range=[self.start,self.final], y_range=[self.start,self.final], plot_width=600, plot_height=600)
        f.image(image=[j.set], x=[self.start], y=[self.start], dw=[self.final-self.start], dh=[self.final-self.start], palette=bokehpalette)
        blt.show(f)

In [11]:
j = JuliaSetPlot(-1.037 + 0.17j)
%time j.set_spacing(0.006)
%time j.show()


CPU times: user 12 ms, sys: 4 ms, total: 16 ms
Wall time: 30 ms
CPU times: user 5.52 s, sys: 148 ms, total: 5.67 s
Wall time: 10 s

In [12]:
j = JuliaSetPlot(-0.624 + 0.435j)
%time j.set_spacing(0.006)
%time j.interact()


CPU times: user 8 ms, sys: 0 ns, total: 8 ms
Wall time: 25 ms
CPU times: user 4.7 s, sys: 36 ms, total: 4.73 s
Wall time: 9.71 s

Other Plots


In [21]:
j = JuliaSetPlot(0.0037 + 0.0017j)
%time j.set_spacing(0.006)
%time j.show()


CPU times: user 8 ms, sys: 0 ns, total: 8 ms
Wall time: 16.6 ms
CPU times: user 10.1 s, sys: 108 ms, total: 10.2 s
Wall time: 15.9 s

In [23]:
j = JuliaSetPlot(1.037 - 0.17j)#changed the value of c
%time j.set_spacing(0.006)
%time j.show()


CPU times: user 12 ms, sys: 0 ns, total: 12 ms
Wall time: 26.9 ms
CPU times: user 2.25 s, sys: 128 ms, total: 2.38 s
Wall time: 4.12 s

In [31]:
j = JuliaSetPlot(-0.624 + 0.435j,4)#set the number of iterations to 4
%time j.set_spacing(0.006)
%time j.interact()


CPU times: user 0 ns, sys: 12 ms, total: 12 ms
Wall time: 11.2 ms
CPU times: user 1.17 s, sys: 28 ms, total: 1.2 s
Wall time: 2.14 s

In [ ]: