-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun3d.py
206 lines (160 loc) · 6.56 KB
/
run3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from numpy import *
from theme.colours import *
from theme.MPLP import *
from library import *
def help_info3d():
string = """
Function_Plotter 3D
-------------------
input function in forms,
y = f(x,z) or f(x) or f(z)
x = f(y,z) or f(y) or f(z)
z = f(x,y) or f(x) or f(y)
Other Commands
exit: quits program
help: opens this page
lib: prints list of current functions in library
"""
print (string)
def run3d():
"""
code that asks user for function and plots it in 3d
"""
#ask for user input
set_tab_complete_options(library_contents+norm_func_tab_list)
func_string_multi = input(output_colour+'input your function:\n'+input_colour)
print(output_colour)
#default lims, if no lims set by user
xfunc_lims = (-25,25)
yfunc_lims = (-25,25)
zfunc_lims = (-25,25)
#non function user inputs
if func_string_multi == 'exit':
exit(0)
if func_string_multi == 'help':
help_info3d()
if func_string_multi == 'lib':
print('Functions in library:\n',library_contents)
run3d()
#set up figure and ax
fig = plt.figure('Function_Plotter',figsize=[7,5])
ax = fig.add_subplot(111, projection='3d')
#colour coding for function
colours = ['red','yellow','white','blue','cyan','pink','purple','red','yellow','white','blue','cyan','pink','purple','red','yellow','white','blue','cyan','pink','purple']
if func_string_multi == 'batman':
colours = ['yellow','yellow','yellow','yellow']
if func_string_multi == 'heart':
colours = ['red','red']
#if function from library call it
if func_string_multi in library_contents:
name = func_string_multi
func_string_multi = eval(func_string_multi)
print("Plotting '{}' from library...".format(name))
#split multiple eqs into single eqs to plot seperatley
for func_string_raw in func_string_multi.split(','):
color = colours[func_string_multi.split(',').index(func_string_raw)]
# hastags denote limits in user inputs
if '#' in func_string_multi:
limits = func_string_raw.split('#')[1:]
#print (limits)
for lim in limits:
# print(lim.split('<'))
# if (len(lim.split('<')) != 3) or (len(lim.split('>')) != 3):
# print('invalid limits, retry:8')
# run3d()
if ('<' in lim) and ( '>' in lim):
print('invalid limits, retry:')
run3d()
if '<' in lim:
lo,var,up = lim.split('<')
if lo == 'pi':
lo =pi
if up =='pi':
up=pi
if '>' in lim:
up,var,lo= lim.split('>')
if lo == 'pi':
lo =pi
if up =='pi':
up=pi
if var == 'x':
#print ('xhigh ={}, xlow={}'.format(up,lo))
xfunc_lims = (float(lo),float(up))
if var == 'y':
yfunc_lims = (float(lo),float(up))
# func_string = func_string_raw.split('=')[1]
# x=float(up)
# upy = eval(func_string)
# x=float(lo)
# loy = eval(func_string)
# print ('yhigh ={}, ylow={}'.format(up,lo))
# yfunc_lims = (float(loy),float(upy))
if var == 'z':
zfunc_lims = (float(lo),float(up))
if '=' not in func_string_raw:
print( 'function requires "=", e.g "y=mx+c"')
run3d()
if ('y =' in func_string_raw) or ('y=' in func_string_raw):
fvar = 'y'
func_string = func_string_raw.split('=')[1]
func_string = alias_check(func_string)
xlist = linspace(xfunc_lims[0],xfunc_lims[1],1000)
zlist = linspace(zfunc_lims[0],zfunc_lims[1],1000)
ylist = []
for n in range (0,len(xlist)):
x = xlist[n]
z = zlist[n]
try:
y = eval(func_string)
ylist.append(y)
except:
print('invalid input, type "help" or retry:')
run3d()
if ('x =' in func_string_raw) or ('x=' in func_string_raw):
fvar = 'x'
func_string = func_string_raw.split('=')[1]
func_string = alias_check(func_string)
ylist = linspace(yfunc_lims[0],yfunc_lims[1],1000)
zlist = linspace(zfunc_lims[0],zfunc_lims[1],1000)
xlist = []
for n in range (0,len(ylist)):
y = ylist[n]
z = zlist[n]
try:
x = eval(func_string)
xlist.append(x)
except:
print('invalid input, type "help" or retry:')
run3d()
if ('z =' in func_string_raw) or ('z=' in func_string_raw):
fvar = 'z'
func_string = func_string_raw.split('=')[1]
func_string = alias_check(func_string)
ylist = linspace(yfunc_lims[0],yfunc_lims[1],1000)
xlist = linspace(xfunc_lims[0],xfunc_lims[1],1000)
zlist = []
for n in range (0,len(ylist)):
x = xlist[n]
y = ylist[n]
try:
z = eval(func_string)
zlist.append(z)
except:
print('invalid input, type "help" or retry:')
run3d()
# title_obj = plt.title('$'+func_string_raw+'$')
if 'x' not in func_string_raw:
xlist = list(zeros(len(xlist)))
if 'y' not in func_string_raw:
ylist = list(zeros(len(ylist)))
if 'z' not in func_string_raw:
zlist = list(zeros(len(zlist)))
legend_label = legend_labeller(func_string_raw)
ax.plot(xlist,ylist,zlist,label=legend_label,c=color)
#after all eqs are plotted apply matplotlib preferences and show figure
MPL_Prefs(fig,ax,'','grid')
plt.show()
run3d()
run3d()