-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrapher_tools.py
More file actions
105 lines (75 loc) · 2.31 KB
/
grapher_tools.py
File metadata and controls
105 lines (75 loc) · 2.31 KB
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
from numpy import sin, cos, linspace, outer, ones, pi, meshgrid
from plotly.graph_objs import Figure, Surface, Layout
import os
def figure_saver(fig, filename, rx, ry, rz):
rs = [rx,ry,rz]
RX, RY, RZ = [i/max(rs) for i in rs]
layout = Layout(
title=filename,
autosize=True,
scene=dict(
aspectmode = 'manual',
aspectratio = {'x': RX, 'y': RY, 'z': RZ},
xaxis_title='x [nm]',
yaxis_title='y [nm]',
zaxis_title='z [nm]',
),
)
# Write the figure to html file and auto open
save_location = f'{os.getcwd()}/{filename}.html'
fig.update_layout(layout)
fig.update_traces(showscale=False)
fig.write_html(save_location, auto_open=True)
print(f'File saved at {save_location}')
def sphere_plotter(filename, rx,ry,rz):
#just a sphere
theta = linspace(0,2*pi,100)
phi = linspace(0,pi,100)
x = outer(cos(theta),sin(phi))
y = outer(sin(theta),sin(phi))
z = outer(ones(100),cos(phi)) # note this is 2d now
x *= rx
y *= ry
z *= rz
data = Surface(
x=x,
y=y,
z=z,
colorscale='plotly3',
opacity=0.9,
)
fig = Figure(data=data)
figure_saver(fig, filename, rx, ry, rz)
# Based on https://community.plotly.com/t/basic-3d-cylinders/27990
def cylinder(r, h, a=0, nt=100, nv = 50):
"""
parametrize the cylinder of radius r, height h, base point a
"""
theta = linspace(0, 2*pi, nt)
v = linspace(a, a + h, nv )
theta, v = meshgrid(theta, v)
x = r*cos(theta)
y = r*sin(theta)
z = v
return x, y, z
# Not working:
def boundary_circle(r, h, nt=100):
"""
r - boundary circle radius
h - height above xy-plane where the circle is included
returns the circle parameterization
"""
theta = linspace(0, 2*pi, nt)
x = r*cos(theta)
y = r*sin(theta)
z = h*ones(theta.shape)
return x, y, z
def cylinder_plotter(filename, r, h, a = 0, nt=100, nv =50):
# Create the cylinder
x, y, z = cylinder(r, h, a=a, nt=nt, nv=nv)
cyl = Surface(x=x, y=y, z=z,
colorscale = 'plotly3',
opacity=0.9
)
fig = Figure(data=[cyl])
figure_saver(fig, filename, r, r, h)