Skip to content

Commit 98a637f

Browse files
committed
Added function visualization toolbox(sorry I'm still an amateur compared to Andre)
1 parent c7fd8b3 commit 98a637f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
215 KB
Loading

FunctionVisualization/visualize.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/home/ocean/anaconda3/bin/python3
2+
#^change the line above to point to your python environment
3+
#This function visualize a function as a bunch of arrows instead of a 2D graph. See https://www.youtube.com/watch?v=CfW845LNObM for details
4+
#The evenly spaced points on the original number line are connected to where they will land in the transformed number line (below and parallel to the original number line.)
5+
#Author: Ocean Wong, May 2018, University of Birmingham.
6+
from numpy import *; from numpy import array as ary; import numpy as np; tau = 2*pi
7+
import matplotlib.pyplot as plt; import matplotlib as mpl
8+
9+
#<Change-able parameters>
10+
Range = ary([-1,1])
11+
def f(x):
12+
return sin(arccos(x))
13+
#Must keep the function on line 12, because of some hack-y function below
14+
#</Change-able parameters>
15+
16+
#Get the function name
17+
Line = open(__file__).readlines()
18+
function = Line[11][8:-1]
19+
functionFileName = function.replace("/", "_over_")#change the forbidden characters to something else.
20+
21+
#plotting
22+
x = np.linspace(Range[0],Range[1], 100)
23+
y = f(x)
24+
z = np.diff( f(x) )/np.diff(x)
25+
Color = np.transpose(
26+
ary([np.linspace(1,1,len(x)), #Make the color vary
27+
np.linspace(0,1,len(x)),
28+
np.linspace(0,0,len(x)),
29+
np.linspace(0.8,0.8,len(x))])
30+
) #transposed such that I can give a list of len(Color[n])==4 when plotting.
31+
32+
fig = plt.figure(facecolor='black')
33+
#fig.patch.set_facecolor('black')
34+
ax = fig.add_subplot(111,facecolor='black')
35+
#ax.set_faceolor
36+
37+
ax.set_aspect(1) #fix aspect ratio
38+
ax.set_xlim(Range) #cut off the excess of the graph
39+
#ax.set_ylim([-1.1,1.1])
40+
ax.set_yticks([]) #vertical case shouldn't have any coordinates.
41+
plt.tight_layout() #same as adding #fig = plt.figure(); fig.set_tight_layout(True)#These two lines needs to be added before ax declaration
42+
43+
option = str(input("Do you want to differentiate (diff) or integrate the function (int)? \n (enter nothing if you want neither)"))
44+
45+
if option == "diff":
46+
z = np.diff( f(x) )/np.diff(x)
47+
for n in range (len(z)):
48+
ax.plot( [y[n],z[n]], [1,0], color = Color[n])
49+
plt.title("differentiate "+function)
50+
#plt.show()
51+
plt.savefig("differentiate_"+functionFileName+".png")
52+
53+
elif option == "int":
54+
dx = np.diff(x)[0]
55+
def Y(y):
56+
Y = np.zeros(len(y))
57+
for n in range (len(y)):
58+
Y[n] = sum(y[0:n])*dx
59+
return Y
60+
z = Y(y)
61+
for n in range (len(z)):
62+
ax.plot( [y[n],z[n]], [1,0], color = Color[n])
63+
plt.title("integrate "+function)
64+
#plt.show()
65+
plt.savefig("integrate_"+functionFileName+".png")
66+
67+
else:
68+
for n in range (len(x)):
69+
ax.plot( [x[n],y[n]] , [1,0], color = Color[n])
70+
plt.title(function)
71+
plt.tight_layout() #same as adding #fig = plt.figure(); fig.set_tight_layout(True)#These two lines needs to be added before
72+
plt.savefig(functionFileName+".png")

0 commit comments

Comments
 (0)