Skip to content

Commit 7b85d58

Browse files
robertodrbast
authored andcommitted
Add some options to plot_cavity.py
1 parent 4e5c80a commit 7b85d58

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

tools/plot_cavity.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
to NumPy format file.
3030
"""
3131

32+
import os
3233
import sys
3334
import docopt
3435
import numpy as np
@@ -39,13 +40,18 @@
3940

4041
options = """
4142
Usage:
42-
./plot_cavity.py <cavity_npz> [--map-by <npy>]
43+
./plot_cavity.py <cavity_npz> [--save <fname> --map-by <npy> --label <label>]
4344
./plot_cavity.py (-h | --help)
4445
4546
Options:
46-
<cavity_npz> Compressed NumPy file with cavity specifications.
47-
--map-by <npy> NumPy format file with surface function to color-map finite elements.
48-
-h --help Show this screen.
47+
<cavity_npz> Compressed NumPy file with cavity specifications.
48+
--save <fname> Filename for the output picture, without extension.
49+
A 600 dpi SVG is produced.
50+
If left empty, the name of the .npz and the label are used.
51+
--map-by <npy> NumPy format file with surface function to color-map finite elements.
52+
--label <label> Label for the mapping colorbar.
53+
If left empty, the NumPy filename will be used.
54+
-h --help Show this screen.
4955
"""
5056

5157

@@ -95,7 +101,7 @@ def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
95101
return newcmap
96102

97103

98-
def plot(cavity_npz, surf_func_npy=None):
104+
def plot(cavity_npz, save, surf_func_npy=None, label=None):
99105
fig = plt.figure()
100106
ax = fig.add_subplot(111, projection='3d')
101107

@@ -105,7 +111,7 @@ def plot(cavity_npz, surf_func_npy=None):
105111
centroids = cavity['centers']
106112

107113
# Plot collocation points
108-
ax.scatter(centroids[0, :], centroids[1, :], centroids[2, :], c='black', alpha=0.5)
114+
ax.scatter(centroids[0, :], centroids[1, :], centroids[2, :], c='black', alpha=0.5, s=5)
109115

110116
# Generate color mapping
111117
colors = (.5, .1, .3, 0.3)
@@ -114,7 +120,8 @@ def plot(cavity_npz, surf_func_npy=None):
114120
shifted_cmap = shiftedColorMap(cm.coolwarm, midpoint=0.75, name='shifted')
115121
mappable = cm.ScalarMappable(cmap=shifted_cmap)
116122
mappable.set_array(surf_func.flatten())
117-
plt.colorbar(mappable)
123+
cb = plt.colorbar(mappable)
124+
cb.set_label(label)
118125
# Provide colors for Poly3DCollection
119126
colors = mappable.to_rgba(surf_func.flatten())
120127
# Generate list of vertices
@@ -126,6 +133,7 @@ def plot(cavity_npz, surf_func_npy=None):
126133
elements = Poly3DCollection(vertices, facecolors=colors)
127134
ax.add_collection3d(elements)
128135
ax.set_axis_off()
136+
plt.savefig(os.path.join(os.getcwd(), save + '.svg'), format='svg', dpi=600, bbox_inches='tight')
129137
plt.show()
130138

131139

@@ -137,8 +145,12 @@ def main():
137145
sys.stderr.write(options)
138146
sys.exit(-1)
139147
cavity_npz = arguments['<cavity_npz>']
148+
cavity = os.path.splitext(cavity_npz)[0]
149+
save = arguments['--save'] if arguments['--save'] else cavity
140150
surf_func_npy = arguments['--map-by']
141-
plot(cavity_npz, surf_func_npy)
151+
surf_func = os.path.splitext(surf_func_npy)[0] if surf_func_npy else ''
152+
label = arguments['--label'] if arguments['--label'] else surf_func
153+
plot(cavity_npz, save, surf_func_npy, label)
142154

143155

144156
if __name__ == '__main__':

0 commit comments

Comments
 (0)