Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
1
2
# coding: utf-8
3
4
# # Julia Set Plotting Extension
5
#
6
# Load module for a JuliaSet that conforms to the specified interface.
7
#
8
# It is wise to run the test suite in `test_juliaset.py` with `nosetests` prior to attempting to plot here.
9
10
# In[1]:
11
12
13
from juliaset import JuliaSet
14
15
16
# Load additional libraries needed for plotting and profiling.
17
18
# In[2]:
19
20
21
# Math libraries
22
import numpy as np
23
from math import sqrt
24
25
# Matplotlib plotting libraries
26
import matplotlib.pyplot as plt
27
get_ipython().run_line_magic('matplotlib', 'inline')
28
29
# Bokeh plotting libraries
30
import bokeh.plotting as blt
31
blt.output_notebook()
32
33
34
# Extend JuliaSet class with additional functionality.
35
36
# In[3]:
37
38
39
class JuliaSetPlot(JuliaSet):
40
"""Extend JuliaSet to add plotting functionality"""
41
42
def __init__(self, *args, **kwargs):
43
# Invoke constructor for JuliaSet first, unaltered
44
super(JuliaSetPlot, self).__init__(*args, **kwargs)
45
# Add one more attribute: a rendered image array
46
self.img = np.array([])
47
48
def get_dim(self):
49
# get what should be an attribute
50
return int(4.0 / self._d)
51
52
def render(self):
53
if not self.set: self.generate()
54
# Convert inefficient list to efficient numpy array
55
self.img = np.array(self.set)
56
dim = self.get_dim()
57
# Reshape array into a 2d complex plane
58
self.img = np.reshape(self.img, (dim,dim)).T
59
60
def show(self):
61
if not self.img.size: self.render()
62
# Specify complex plane axes efficiently
63
xy = np.linspace(-2,2,self.get_dim())
64
# Use matplotlib to plot image as an efficient mesh
65
plt.figure(1, figsize=(12,9))
66
plt.pcolormesh(xy,xy,self.img, cmap=plt.cm.hot)
67
plt.colorbar()
68
plt.show()
69
70
def interact(self):
71
from matplotlib.colors import rgb2hex
72
if not self.img.size: self.render()
73
# Mimic matplotlib "hot" color palette
74
colormap = plt.cm.get_cmap("hot")
75
bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]
76
# Use bokeh to plot an interactive image
77
f = blt.figure(x_range=[-2,2], y_range=[-2,2], plot_width=600, plot_height=600)
78
f.image(image=[j.img], x=[-2,2], y=[-2,2], dw=[4], dh=[4], palette=bokehpalette)
79
blt.show(f)
80
81
82
# Visualize a Julia set using matplotlib.
83
84
# In[4]:
85
86
87
j = JuliaSetPlot(-1.037 + 0.17j)
88
get_ipython().run_line_magic('time', 'j.set_spacing(0.006)')
89
get_ipython().run_line_magic('time', 'j.show()')
90
91
92
# Visualize a different Julia set using Bokeh as an interactive Javascript plot.
93
94
# In[5]:
95
96
97
j = JuliaSetPlot(-0.624 + 0.435j)
98
get_ipython().run_line_magic('time', 'j.set_spacing(0.006)')
99
get_ipython().run_line_magic('time', 'j.interact()')
100
101
102
# In[6]:
103
104
105
get_ipython().run_line_magic('prun', 'j.generate()')
106
107
108
# In[7]:
109
110
111
get_ipython().run_line_magic('load_ext', 'line_profiler')
112
get_ipython().run_line_magic('lprun', '-f j.generate j.generate()')
113
114
115