Skip to content

Commit b25b7a6

Browse files
committed
first attempt to add the "transpose" argument all 2D plot-related tidy3d functions
1 parent e902f77 commit b25b7a6

File tree

11 files changed

+340
-199
lines changed

11 files changed

+340
-199
lines changed

tidy3d/components/base_sim/simulation.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def plot(
251251
hlim: Optional[tuple[float, float]] = None,
252252
vlim: Optional[tuple[float, float]] = None,
253253
fill_structures: bool = True,
254+
transpose: bool = False,
254255
**patch_kwargs,
255256
) -> Ax:
256257
"""Plot each of simulation's components on a plane defined by one nonzero x,y,z coordinate.
@@ -282,23 +283,23 @@ def plot(
282283
"""
283284

284285
hlim, vlim = Scene._get_plot_lims(
285-
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
286+
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
286287
)
287288

288289
ax = self.scene.plot_structures(
289-
ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, fill=fill_structures
290+
ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, fill=fill_structures, transpose=transpose
290291
)
291-
ax = self.plot_sources(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=source_alpha)
292-
ax = self.plot_monitors(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=monitor_alpha)
292+
ax = self.plot_sources(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=source_alpha, transpose=transpose)
293+
ax = self.plot_monitors(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=monitor_alpha, transpose=transpose)
293294
ax = Scene._set_plot_bounds(
294-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
295+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
295296
)
296-
ax = self.plot_boundaries(ax=ax, x=x, y=y, z=z)
297-
ax = self.plot_symmetries(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim)
297+
ax = self.plot_boundaries(ax=ax, x=x, y=y, z=z, transpose=transpose)
298+
ax = self.plot_symmetries(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose)
298299

299300
# Add the default axis labels, tick labels, and title
300301
ax = Box.add_ax_labels_and_title(
301-
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units
302+
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units, transpose=transpose
302303
)
303304

304305
return ax
@@ -314,6 +315,7 @@ def plot_sources(
314315
vlim: Optional[tuple[float, float]] = None,
315316
alpha: Optional[float] = None,
316317
ax: Ax = None,
318+
transpose: bool = False,
317319
) -> Ax:
318320
"""Plot each of simulation's sources on a plane defined by one nonzero x,y,z coordinate.
319321
@@ -341,13 +343,13 @@ def plot_sources(
341343
"""
342344
bounds = self.bounds
343345
for source in self.sources:
344-
ax = source.plot(x=x, y=y, z=z, alpha=alpha, ax=ax, sim_bounds=bounds)
346+
ax = source.plot(x=x, y=y, z=z, alpha=alpha, ax=ax, sim_bounds=bounds, transpose=transpose)
345347
ax = Scene._set_plot_bounds(
346-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
348+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
347349
)
348350
# Add the default axis labels, tick labels, and title
349351
ax = Box.add_ax_labels_and_title(
350-
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units
352+
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units, transpose=transpose
351353
)
352354
return ax
353355

@@ -362,6 +364,7 @@ def plot_monitors(
362364
vlim: Optional[tuple[float, float]] = None,
363365
alpha: Optional[float] = None,
364366
ax: Ax = None,
367+
transpose: bool = False,
365368
) -> Ax:
366369
"""Plot each of simulation's monitors on a plane defined by one nonzero x,y,z coordinate.
367370
@@ -389,13 +392,13 @@ def plot_monitors(
389392
"""
390393
bounds = self.bounds
391394
for monitor in self.monitors:
392-
ax = monitor.plot(x=x, y=y, z=z, alpha=alpha, ax=ax, sim_bounds=bounds)
395+
ax = monitor.plot(x=x, y=y, z=z, alpha=alpha, ax=ax, sim_bounds=bounds, transpose=transpose)
393396
ax = Scene._set_plot_bounds(
394-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
397+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
395398
)
396399
# Add the default axis labels, tick labels, and title
397400
ax = Box.add_ax_labels_and_title(
398-
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units
401+
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units, transpose=transpose
399402
)
400403
return ax
401404

@@ -409,6 +412,7 @@ def plot_symmetries(
409412
hlim: Optional[tuple[float, float]] = None,
410413
vlim: Optional[tuple[float, float]] = None,
411414
ax: Ax = None,
415+
transpose: bool = False,
412416
) -> Ax:
413417
"""Plot each of simulation's symmetries on a plane defined by one nonzero x,y,z coordinate.
414418
@@ -432,21 +436,21 @@ def plot_symmetries(
432436
matplotlib.axes._subplots.Axes
433437
The supplied or created matplotlib axes.
434438
"""
435-
439+
9
436440
normal_axis, _ = Box.parse_xyz_kwargs(x=x, y=y, z=z)
437441

438442
for sym_axis, sym_value in enumerate(self.symmetry):
439443
if sym_value == 0 or sym_axis == normal_axis:
440444
continue
441445
sym_box = self._make_symmetry_box(sym_axis=sym_axis)
442446
plot_params = self._make_symmetry_plot_params(sym_value=sym_value)
443-
ax = sym_box.plot(x=x, y=y, z=z, ax=ax, **plot_params.to_kwargs())
447+
ax = sym_box.plot(x=x, y=y, z=z, ax=ax, transpose=transpose, **plot_params.to_kwargs())
444448
ax = Scene._set_plot_bounds(
445-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
449+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
446450
)
447451
# Add the default axis labels, tick labels, and title
448452
ax = Box.add_ax_labels_and_title(
449-
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units
453+
ax=ax, x=x, y=y, z=z, plot_length_units=self.plot_length_units, transpose=transpose
450454
)
451455
return ax
452456

@@ -482,6 +486,7 @@ def plot_boundaries(
482486
y: Optional[float] = None,
483487
z: Optional[float] = None,
484488
ax: Ax = None,
489+
transpose: bool = False,
485490
**kwargs,
486491
) -> Ax:
487492
"""Plot the simulation boundary conditions as lines on a plane
@@ -519,6 +524,7 @@ def plot_structures(
519524
hlim: Optional[tuple[float, float]] = None,
520525
vlim: Optional[tuple[float, float]] = None,
521526
fill: bool = True,
527+
transpose: bool = False,
522528
) -> Ax:
523529
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
524530
@@ -545,11 +551,11 @@ def plot_structures(
545551
"""
546552

547553
hlim_new, vlim_new = Scene._get_plot_lims(
548-
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
554+
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
549555
)
550556

551557
return self.scene.plot_structures(
552-
x=x, y=y, z=z, ax=ax, hlim=hlim_new, vlim=vlim_new, fill=fill
558+
x=x, y=y, z=z, ax=ax, hlim=hlim_new, vlim=vlim_new, fill=fill, transpose=transpose
553559
)
554560

555561
@equal_aspect
@@ -566,6 +572,7 @@ def plot_structures_eps(
566572
ax: Ax = None,
567573
hlim: Optional[tuple[float, float]] = None,
568574
vlim: Optional[tuple[float, float]] = None,
575+
transpose: bool = False,
569576
) -> Ax:
570577
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
571578
The permittivity is plotted in grayscale based on its value at the specified frequency.
@@ -603,7 +610,7 @@ def plot_structures_eps(
603610
"""
604611

605612
hlim, vlim = Scene._get_plot_lims(
606-
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
613+
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
607614
)
608615

609616
return self.scene.plot_structures_eps(
@@ -617,6 +624,7 @@ def plot_structures_eps(
617624
hlim=hlim,
618625
vlim=vlim,
619626
reverse=reverse,
627+
transpose=transpose,
620628
)
621629

622630
@equal_aspect
@@ -632,6 +640,7 @@ def plot_structures_heat_conductivity(
632640
ax: Ax = None,
633641
hlim: Optional[tuple[float, float]] = None,
634642
vlim: Optional[tuple[float, float]] = None,
643+
transpose: bool = False,
635644
) -> Ax:
636645
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
637646
The permittivity is plotted in grayscale based on its value at the specified frequency.
@@ -669,7 +678,7 @@ def plot_structures_heat_conductivity(
669678
"""
670679

671680
hlim, vlim = Scene._get_plot_lims(
672-
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
681+
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
673682
)
674683

675684
return self.scene.plot_structures_heat_conductivity(
@@ -682,6 +691,7 @@ def plot_structures_heat_conductivity(
682691
hlim=hlim,
683692
vlim=vlim,
684693
reverse=reverse,
694+
transpose=transpose,
685695
)
686696

687697
@classmethod

tidy3d/components/eme/simulation.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ def plot_eme_ports(
306306
ax: Ax = None,
307307
hlim: Optional[tuple[float, float]] = None,
308308
vlim: Optional[tuple[float, float]] = None,
309+
transpose: bool = False,
309310
**kwargs,
310311
) -> Ax:
311312
"""Plot the EME ports."""
@@ -315,15 +316,15 @@ def plot_eme_ports(
315316
rmax = self.geometry.bounds[1][self.axis]
316317
ports = np.array([rmin + self.port_offsets[0], rmax - self.port_offsets[1]])
317318
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
318-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis)
319+
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, transpose=transpose)
319320
boundaries_x = []
320321
boundaries_y = []
321322
if axis_x == self.axis:
322323
boundaries_x = ports
323324
if axis_y == self.axis:
324325
boundaries_y = ports
325-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis)
326-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis)
326+
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, transpose=transpose)
327+
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, transpose=transpose)
327328
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
328329
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
329330
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]
@@ -334,7 +335,7 @@ def plot_eme_ports(
334335
ax.add_collection(line_segments_y)
335336

336337
ax = Scene._set_plot_bounds(
337-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
338+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
338339
)
339340

340341
return ax
@@ -350,6 +351,7 @@ def plot_eme_subgrid_boundaries(
350351
ax: Ax = None,
351352
hlim: Optional[tuple[float, float]] = None,
352353
vlim: Optional[tuple[float, float]] = None,
354+
transpose: bool = False,
353355
**kwargs,
354356
) -> Ax:
355357
"""Plot the EME subgrid boundaries.
@@ -363,15 +365,15 @@ def plot_eme_subgrid_boundaries(
363365
subgrid_boundaries = np.array(eme_grid_spec.subgrid_boundaries)
364366
subgrids = eme_grid_spec.subgrids
365367
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
366-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis)
368+
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, transpose=transpose)
367369
boundaries_x = []
368370
boundaries_y = []
369371
if axis_x == self.axis:
370372
boundaries_x = subgrid_boundaries
371373
if axis_y == self.axis:
372374
boundaries_y = subgrid_boundaries
373-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis)
374-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis)
375+
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, transpose=transpose)
376+
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, transpose=transpose)
375377
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
376378
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
377379
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]
@@ -382,12 +384,12 @@ def plot_eme_subgrid_boundaries(
382384
ax.add_collection(line_segments_y)
383385

384386
ax = Scene._set_plot_bounds(
385-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
387+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
386388
)
387389

388390
for subgrid in subgrids:
389391
ax = self.plot_eme_subgrid_boundaries(
390-
eme_grid_spec=subgrid, x=x, y=y, z=z, ax=ax, hlim=hlim, vlim=vlim, **kwargs
392+
eme_grid_spec=subgrid, x=x, y=y, z=z, ax=ax, hlim=hlim, vlim=vlim, transpose=transpose, **kwargs
391393
)
392394

393395
return ax
@@ -402,22 +404,23 @@ def plot_eme_grid(
402404
ax: Ax = None,
403405
hlim: Optional[tuple[float, float]] = None,
404406
vlim: Optional[tuple[float, float]] = None,
407+
transpose: bool = False,
405408
**kwargs,
406409
) -> Ax:
407410
"""Plot the EME grid."""
408411
kwargs.setdefault("linewidth", 0.2)
409412
kwargs.setdefault("colors", "black")
410413
cell_boundaries = self.eme_grid.boundaries
411414
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
412-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis)
415+
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, transpose=transpose)
413416
boundaries_x = []
414417
boundaries_y = []
415418
if axis_x == self.axis:
416419
boundaries_x = cell_boundaries
417420
if axis_y == self.axis:
418421
boundaries_y = cell_boundaries
419-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis)
420-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis)
422+
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, transpose=transpose)
423+
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, transpose=transpose)
421424
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
422425
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
423426
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]
@@ -428,7 +431,7 @@ def plot_eme_grid(
428431
ax.add_collection(line_segments_y)
429432

430433
ax = Scene._set_plot_bounds(
431-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
434+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
432435
)
433436

434437
return ax
@@ -445,6 +448,7 @@ def plot(
445448
monitor_alpha: Optional[float] = None,
446449
hlim: Optional[tuple[float, float]] = None,
447450
vlim: Optional[tuple[float, float]] = None,
451+
transpose: bool = False,
448452
**patch_kwargs,
449453
) -> Ax:
450454
"""Plot each of simulation's components on a plane defined by one nonzero x,y,z coordinate.
@@ -475,23 +479,23 @@ def plot(
475479
"""
476480

477481
hlim, vlim = Scene._get_plot_lims(
478-
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
482+
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
479483
)
480484

481-
ax = self.scene.plot_structures(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim)
482-
ax = self.plot_sources(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=source_alpha)
483-
ax = self.plot_monitors(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=monitor_alpha)
485+
ax = self.scene.plot_structures(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose)
486+
ax = self.plot_sources(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=source_alpha, transpose=transpose)
487+
ax = self.plot_monitors(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, alpha=monitor_alpha, transpose=transpose)
484488
ax = Scene._set_plot_bounds(
485-
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
489+
bounds=self.simulation_bounds, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
486490
)
487-
ax = self.plot_boundaries(ax=ax, x=x, y=y, z=z)
488-
ax = self.plot_symmetries(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim)
491+
ax = self.plot_boundaries(ax=ax, x=x, y=y, z=z, transpose=transpose)
492+
ax = self.plot_symmetries(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose)
489493

490-
ax = self.plot_eme_grid(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim)
494+
ax = self.plot_eme_grid(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose)
491495
ax = self.plot_eme_subgrid_boundaries(
492-
eme_grid_spec=self.eme_grid_spec, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim
496+
eme_grid_spec=self.eme_grid_spec, ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose
493497
)
494-
ax = self.plot_eme_ports(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim)
498+
ax = self.plot_eme_ports(ax=ax, x=x, y=y, z=z, hlim=hlim, vlim=vlim, transpose=transpose)
495499
return ax
496500

497501
@cached_property

0 commit comments

Comments
 (0)