Skip to content

Commit de1495a

Browse files
committed
Improved plotting for multiphase microstructures
1 parent 510cd31 commit de1495a

File tree

2 files changed

+74
-28
lines changed

2 files changed

+74
-28
lines changed

src/kanapy/api.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,24 @@ def generate_orientations(self, data, ang=None, omega=None, Nbase=5000,
370370
-------- Plotting methods --------
371371
"""
372372

373-
def plot_ellipsoids(self, cmap='prism', dual_phase=False):
373+
def plot_ellipsoids(self, cmap='prism', dual_phase=None, phases=False):
374374
""" Generates plot of particles"""
375+
if dual_phase is not None:
376+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
377+
phases = dual_phase
375378
if self.particles is None:
376379
raise ValueError('No particle to plot. Run pack first.')
377380
hmin = min(self.rve.size)
378381
asp_arr = [int(self.rve.size[0] / hmin),
379382
int(self.rve.size[1] / hmin),
380383
int(self.rve.size[2] / hmin)]
381-
plot_ellipsoids_3D(self.particles, cmap=cmap, dual_phase=dual_phase, asp_arr=asp_arr)
384+
plot_ellipsoids_3D(self.particles, cmap=cmap, phases=phases, asp_arr=asp_arr)
382385

383-
def plot_particles(self, cmap='prism', dual_phase=False, plot_hull=True):
386+
def plot_particles(self, cmap='prism', dual_phase=None, phases=False, plot_hull=True):
384387
""" Generates plot of particles"""
388+
if dual_phase is not None:
389+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
390+
phases = dual_phase
385391
if self.particles is None:
386392
raise ValueError('No particle to plot. Run pack first.')
387393
if self.particles[0].inner is None:
@@ -391,9 +397,9 @@ def plot_particles(self, cmap='prism', dual_phase=False, plot_hull=True):
391397
int(self.rve.size[1] / hmin),
392398
int(self.rve.size[2] / hmin)]
393399
plot_particles_3D(self.particles, cmap=cmap,
394-
dual_phase=dual_phase, plot_hull=plot_hull, asp_arr=asp_arr)
400+
phases=phases, plot_hull=plot_hull, asp_arr=asp_arr)
395401

396-
def plot_voxels(self, sliced=False, dual_phase=False, cmap='prism', ori=None,
402+
def plot_voxels(self, sliced=False, dual_phase=None, phases=False, cmap='prism', ori=None,
397403
color_key=0, silent=False):
398404
"""
399405
Generates plot of voxel structure
@@ -412,9 +418,12 @@ def plot_voxels(self, sliced=False, dual_phase=False, cmap='prism', ori=None,
412418
-------
413419
414420
"""
421+
if dual_phase is not None:
422+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
423+
phases = dual_phase
415424
if self.mesh.grains is None:
416425
raise ValueError('No voxels or elements to plot. Run voxelize first.')
417-
elif dual_phase:
426+
elif phases:
418427
data = self.mesh.phases
419428
else:
420429
data = self.mesh.grains
@@ -435,14 +444,19 @@ def plot_voxels(self, sliced=False, dual_phase=False, cmap='prism', ori=None,
435444
int(self.rve.size[1] / hmin),
436445
int(self.rve.size[2] / hmin)]
437446
fig = plot_voxels_3D(data, Ngr=np.sum(self.ngrains), sliced=sliced,
438-
dual_phase=dual_phase, cmap=cmap, clist=clist,
447+
phases=phases, cmap=cmap, clist=clist,
439448
silent=silent, asp_arr=asp_arr)
440449
if silent:
441450
return fig
442451

443452
def plot_grains(self, geometry=None, cmap='prism', alpha=0.4,
444-
ec=[0.5, 0.5, 0.5, 0.1], dual_phase=False):
453+
ec=None, dual_phase=None, phases=False):
445454
""" Plot polygonalized microstructure"""
455+
if ec is None:
456+
ec = [0.5, 0.5, 0.5, 0.1]
457+
if dual_phase is not None:
458+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
459+
phases = dual_phase
446460
if geometry is None:
447461
geometry = self.geometry
448462
if geometry is None:
@@ -452,23 +466,26 @@ def plot_grains(self, geometry=None, cmap='prism', alpha=0.4,
452466
int(self.rve.size[1] / hmin),
453467
int(self.rve.size[2] / hmin)]
454468
plot_polygons_3D(geometry, cmap=cmap, alpha=alpha, ec=ec,
455-
dual_phase=dual_phase, asp_arr=asp_arr)
469+
phases=phases, asp_arr=asp_arr)
456470

457471
def plot_stats(self, data=None,
458472
gs_data=None, gs_param=None,
459473
ar_data=None, ar_param=None,
460-
dual_phase=False,
474+
dual_phase=None, phases=False,
461475
save_files=False,
462476
show_all=False, verbose=False,
463477
silent=False, enhanced_plot=False):
464478
""" Plots the particle- and grain diameter attributes for statistical
465479
comparison."""
480+
if dual_phase is not None:
481+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
482+
phases = dual_phase
466483
if silent:
467484
verbose = False
468485
show_all = False
469486
enhanced_plot = True
470487
ax_max = np.prod(self.rve.size) ** (1 / 3)
471-
if dual_phase:
488+
if phases:
472489
nphases = self.nphases
473490
if self.precipit and 0 in self.mesh.grain_dict.keys():
474491
# in case of precipit, remove irregular grain 0 from analysis
@@ -494,7 +511,7 @@ def plot_stats(self, data=None,
494511
for ip in range(nphases):
495512
stats_list = []
496513
labels = []
497-
if dual_phase:
514+
if phases:
498515
iphase = ip
499516
print(f'Plotting statistical information for phase {ip}')
500517

@@ -537,7 +554,7 @@ def plot_stats(self, data=None,
537554
stats_list.append(grain_stats)
538555
labels.append('Grains')
539556

540-
if dual_phase:
557+
if phases:
541558
print(f'\nStatistical microstructure parameters of phase {iphase} in RVE')
542559
print('-------------------------------------------------------')
543560
else:

src/kanapy/plotting.py

+44-15
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
# return matplotlib_dpi
3636

3737

38-
def plot_voxels_3D(data, Ngr=1, sliced=False, dual_phase=False,
38+
def plot_voxels_3D(data, Ngr=None, sliced=False, dual_phase=None,
3939
mask=None, cmap='prism', alpha=1.0, silent=False,
40-
clist=None, asp_arr=None):
40+
clist=None, asp_arr=None,
41+
phases=False, cols=None):
4142
"""
4243
Plot voxels in microstructure, each grain with a different color. Sliced
4344
indicates whether one eighth of the box should be removed to see internal
@@ -72,6 +73,13 @@ def plot_voxels_3D(data, Ngr=1, sliced=False, dual_phase=False,
7273
Axes handle for 3D subplot
7374
7475
"""
76+
if dual_phase is not None:
77+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
78+
phases = dual_phase
79+
if Ngr is not None:
80+
print('Use of "Ngr" is depracted. Value will be determined automatically.')
81+
if cols is None:
82+
cols = ['red', 'green', 'lightblue', 'orange', 'gray']
7583
Nx = data.shape[0]
7684
Ny = data.shape[1]
7785
Nz = data.shape[2]
@@ -81,8 +89,8 @@ def plot_voxels_3D(data, Ngr=1, sliced=False, dual_phase=False,
8189
vox_b = np.full(data.shape, True, dtype=bool)
8290
else:
8391
vox_b = mask
84-
if dual_phase:
85-
Ngr = 2
92+
if phases:
93+
Ngr = len(np.unique(data))
8694
else:
8795
Ngr = np.max(data)
8896
if np.min(data) == 0:
@@ -128,8 +136,9 @@ def plot_voxels_3D(data, Ngr=1, sliced=False, dual_phase=False,
128136
plt.show(block=True)
129137

130138

131-
def plot_polygons_3D(geometry, cmap='prism', alpha=0.4, ec=[0.5, 0.5, 0.5, 0.1],
132-
dual_phase=False, silent=False, asp_arr=None):
139+
def plot_polygons_3D(geometry, cmap='prism', alpha=0.4, ec=None,
140+
dual_phase=None, silent=False, asp_arr=None,
141+
phases=False, cols=None):
133142
"""
134143
Plot triangularized convex hulls of grains, based on vertices, i.e.
135144
connection points of 4 up to 8 grains or the end points of triple or quadruple
@@ -154,6 +163,13 @@ def plot_polygons_3D(geometry, cmap='prism', alpha=0.4, ec=[0.5, 0.5, 0.5, 0.1],
154163
None.
155164
156165
"""
166+
if dual_phase is not None:
167+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
168+
phases = dual_phase
169+
if cols is None:
170+
cols = ['red', 'green', 'lightblue', 'orange', 'gray']
171+
if ec is None:
172+
ec = [0.5, 0.5, 0.5, 0.1]
157173
if asp_arr is None:
158174
asp_arr = [1, 1, 1]
159175
grains = geometry['Grains']
@@ -165,11 +181,12 @@ def plot_polygons_3D(geometry, cmap='prism', alpha=0.4, ec=[0.5, 0.5, 0.5, 0.1],
165181
for igr in grains.keys():
166182
if not grains[igr]['Simplices']:
167183
continue
168-
if dual_phase:
169-
if grains[igr]['Phase'] == 0:
170-
col = 'red'
184+
if phases:
185+
icol = grains[igr]['Phase']
186+
if icol < len(cols):
187+
col = cols[icol]
171188
else:
172-
col = 'green'
189+
col = 'gray'
173190
else:
174191
col = list(cm(igr))
175192
col[-1] = alpha # change alpha channel to create semi-transparency
@@ -187,7 +204,8 @@ def plot_polygons_3D(geometry, cmap='prism', alpha=0.4, ec=[0.5, 0.5, 0.5, 0.1],
187204

188205

189206

190-
def plot_ellipsoids_3D(particles, cmap='prism', dual_phase=False, silent=False, asp_arr=None):
207+
def plot_ellipsoids_3D(particles, cmap='prism', dual_phase=None, silent=False, asp_arr=None,
208+
phases=False, cols=None):
191209
"""
192210
Display ellipsoids after packing procedure
193211
Parameters
@@ -201,18 +219,27 @@ def plot_ellipsoids_3D(particles, cmap='prism', dual_phase=False, silent=False,
201219
"""
202220
if asp_arr is None:
203221
asp_arr = [1, 1, 1]
222+
if dual_phase is not None:
223+
print('Use of "dual_phase" is depracted. Use parameter "phases" instead.')
224+
phases = dual_phase
225+
if cols is None:
226+
cols = ['red', 'green', 'lightblue', 'orange', 'gray']
204227
fig = plt.figure(figsize=(6, 6))
205228
ax = fig.add_subplot(111, projection='3d')
206229
ax.set(xlabel='x', ylabel='y', zlabel='z')
207230
ax.view_init(30, 30)
208231
Npa = len(particles)
209-
cm = plt.get_cmap(cmap, Npa+1) if not dual_phase else None
232+
cm = plt.get_cmap(cmap, Npa+1) if not phases else None
210233

211234
for i, pa in enumerate(particles):
212235
if pa.duplicate:
213236
continue
214-
if dual_phase:
215-
color = 'red' if pa.phasenum == 0 else 'green'
237+
if phases:
238+
icol = pa.phasenum
239+
if icol < len(cols):
240+
color = cols[icol]
241+
else:
242+
color = 'gray'
216243
else:
217244
color = cm(i + 1)
218245
pts = pa.surfacePointsGen(nang=100)
@@ -269,8 +296,10 @@ def plot_particles_3D(particles, cmap='prism', dual_phase=False, plot_hull=True,
269296
if dual_phase:
270297
if pa.phasenum == 0:
271298
col = 'red'
272-
else:
299+
elif pa.phasenum == 1:
273300
col = 'green'
301+
else:
302+
col = 'lightblue'
274303
else:
275304
col = cm(i + 1) # set to 'b' for only blue ellipsoids
276305
pa.sync_poly()

0 commit comments

Comments
 (0)