Folder full of pertinent coursework
1# coding: utf-823# # Julia Set Plotting Extension4#5# Load module for a JuliaSet that conforms to the specified interface.6#7# It is wise to run the test suite in `test_juliaset.py` with `nosetests` prior to attempting to plot here.89# In[1]:101112from juliaset import JuliaSet131415# Load additional libraries needed for plotting and profiling.1617# In[2]:181920# Math libraries21import numpy as np22from math import sqrt2324# Matplotlib plotting libraries25import matplotlib.pyplot as plt26get_ipython().run_line_magic('matplotlib', 'inline')2728# Bokeh plotting libraries29import bokeh.plotting as blt30blt.output_notebook()313233# Extend JuliaSet class with additional functionality.3435# In[3]:363738class JuliaSetPlot(JuliaSet):39"""Extend JuliaSet to add plotting functionality"""4041def __init__(self, *args, **kwargs):42# Invoke constructor for JuliaSet first, unaltered43super(JuliaSetPlot, self).__init__(*args, **kwargs)44# Add one more attribute: a rendered image array45self.img = np.array([])4647def get_dim(self):48# get what should be an attribute49return int(4.0 / self._d)5051def render(self):52if not self.set: self.generate()53# Convert inefficient list to efficient numpy array54self.img = np.array(self.set)55dim = self.get_dim()56# Reshape array into a 2d complex plane57self.img = np.reshape(self.img, (dim,dim)).T5859def show(self):60if not self.img.size: self.render()61# Specify complex plane axes efficiently62xy = np.linspace(-2,2,self.get_dim())63# Use matplotlib to plot image as an efficient mesh64plt.figure(1, figsize=(12,9))65plt.pcolormesh(xy,xy,self.img, cmap=plt.cm.hot)66plt.colorbar()67plt.show()6869def interact(self):70from matplotlib.colors import rgb2hex71if not self.img.size: self.render()72# Mimic matplotlib "hot" color palette73colormap = plt.cm.get_cmap("hot")74bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]75# Use bokeh to plot an interactive image76f = blt.figure(x_range=[-2,2], y_range=[-2,2], plot_width=600, plot_height=600)77f.image(image=[j.img], x=[-2,2], y=[-2,2], dw=[4], dh=[4], palette=bokehpalette)78blt.show(f)798081# Visualize a Julia set using matplotlib.8283# In[4]:848586j = JuliaSetPlot(-1.037 + 0.17j)87get_ipython().run_line_magic('time', 'j.set_spacing(0.006)')88get_ipython().run_line_magic('time', 'j.show()')899091# Visualize a different Julia set using Bokeh as an interactive Javascript plot.9293# In[5]:949596j = JuliaSetPlot(-0.624 + 0.435j)97get_ipython().run_line_magic('time', 'j.set_spacing(0.006)')98get_ipython().run_line_magic('time', 'j.interact()')99100101# In[6]:102103104get_ipython().run_line_magic('prun', 'j.generate()')105106107# In[7]:108109110get_ipython().run_line_magic('load_ext', 'line_profiler')111get_ipython().run_line_magic('lprun', '-f j.generate j.generate()')112113114115