-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvisuals.py
70 lines (60 loc) · 2.45 KB
/
visuals.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import isnan
plt.style.use('bmh') # Use some nicer default colors
def plot_coords(coords, array=True, bare_plot=False):
'''
Plots a list of coordinates in 3D.
Parameters:
coords (list or ndarray): A list of coordinates. If `array` is `True`,
`coords` should be an ndarray with shape (3, N).
If `array` is `False`, `coords` should be a list of
tuples containing the X, Y, Z, alpha, beta, diam,
ect. values for each coordinate.
array (bool): Determines if `coords` is an ndarray or a list. Default is `True`.
bare_plot (bool): Determines if the plot should be bare or not. Default is `False`.
If `True`, axis markers are turned off.
Returns:
None
'''
# Create the plot and configure it appropriately.
ax = plt.figure()
if bare_plot:
# Turns off the axis markers.
ax = plt.axis('off')
else:
# Ensures equal aspect ratio.
ax = ax.gca(projection='3d')
if array:
# Loop over each coordinate and plot it.
for i in range(coords.shape[1]-1):
ax.plot(coords[0,i:i+2], coords[1,i:i+2], coords[2,i:i+2], color='blue')
else:
# Unpack the coordinates into separate arrays.
X, Y, Z, alpha, beta, diam, _, _, _, _ = zip(*coords)
X = np.array(X)
Y = np.array(Y)
Z = np.array(Z)
diam = np.array(diam)
# Loop over each coordinate and plot it.
for i in range(len(X)-1):
ax.plot(X[i:i+2], Y[i:i+2], Z[i:i+2], linewidth=0.5*diam[i], color='blue')
# Show the plot.
plt.show()
def print_coords(coords):
"""
Prints the coordinates provided, with gaps for any `NaN` values.
Parameters:
coords (list): A list of coordinates to be printed. Each coordinate should be a tuple of (x, y, z, alpha, beta, diam, label).
Returns:
None
"""
# Iterate through each coordinate in the list.
for (x, y, z, _, _, _, _) in coords:
if isnan(x):
# If there is a `NaN` value for the x-coordinate, print a gap.
print('<gap>')
else:
# Otherwise, print the x, y, and z-coordinates rounded to 2 decimal places.
print('({:.2f}, {:.2f}, {:.2f})'.format(x, y, z))