From b4a3734a499a50e6d838e6d82647f79077b4ce35 Mon Sep 17 00:00:00 2001 From: momchil Date: Thu, 24 Mar 2022 14:38:02 -0700 Subject: [PATCH] Updating eps plotting to make background medium transparent and optionally reverse the colorbar --- tidy3d/components/data.py | 10 +++++++++- tidy3d/components/simulation.py | 14 ++++++++------ tidy3d/components/viz.py | 6 ++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tidy3d/components/data.py b/tidy3d/components/data.py index f08436b5a3..7640f48281 100644 --- a/tidy3d/components/data.py +++ b/tidy3d/components/data.py @@ -918,8 +918,10 @@ def plot_field_array( if val == "abs": cmap = "magma" + eps_reverse = False else: cmap = "RdBu" + eps_reverse = True # plot the field xy_coord_labels = list("xyz") @@ -929,7 +931,13 @@ def plot_field_array( # plot the simulation epsilon ax = self.simulation.plot_structures_eps( - freq=freq, cbar=False, alpha=eps_alpha, ax=ax, **{axis_label: position}, **patch_kwargs + freq=freq, + cbar=False, + alpha=eps_alpha, + reverse=eps_reverse, + ax=ax, + **{axis_label: position}, + **patch_kwargs, ) # set the limits based on the xarray coordinates min and max diff --git a/tidy3d/components/simulation.py b/tidy3d/components/simulation.py index d4aec37fea..0cd8da6783 100644 --- a/tidy3d/components/simulation.py +++ b/tidy3d/components/simulation.py @@ -652,8 +652,7 @@ def plot_structures( params_updater = StructMediumParams(medium=medium, medium_map=medium_map) kwargs_struct = params_updater.update_params(**kwargs) if medium == self.medium: - kwargs_struct["edgecolor"] = "white" - kwargs_struct["facecolor"] = "white" + continue patch = PolygonPatch(shape, **kwargs_struct) ax.add_artist(patch) ax = self._set_plot_bounds(ax=ax, x=x, y=y, z=z) @@ -683,6 +682,7 @@ def plot_structures_eps( # pylint: disable=too-many-arguments,too-many-locals z: float = None, freq: float = None, cbar: bool = True, + reverse: bool = True, ax: Ax = None, **kwargs, ) -> Ax: @@ -701,6 +701,8 @@ def plot_structures_eps( # pylint: disable=too-many-arguments,too-many-locals freq : float = None Frequency to evaluate the relative permittivity of all mediums. If not specified, evaluates at infinite frequency. + reverse : bool = True + If ``True``, the highest permittivity is plotted in black; if ``False``: white. ax : matplotlib.axes._subplots.Axes = None Matplotlib axes to plot on, if not specified, one is created. **kwargs @@ -725,10 +727,12 @@ def plot_structures_eps( # pylint: disable=too-many-arguments,too-many-locals medium_shapes = self._filter_structures_plane(self.structures, x=x, y=y, z=z) for (medium, shape) in medium_shapes: eps = medium.eps_model(freq).real - params_updater = StructEpsParams(eps=eps, eps_min=eps_min, eps_max=eps_max) + params_updater = StructEpsParams( + eps=eps, eps_min=eps_min, eps_max=eps_max, reverse=reverse + ) kwargs_struct = params_updater.update_params(**kwargs) if medium == self.medium: - kwargs_struct["edgecolor"] = "white" + continue patch = PolygonPatch(shape, **kwargs_struct) ax.add_artist(patch) if cbar: @@ -1043,8 +1047,6 @@ def _filter_structures_plane( # loop through background_shapes (note: all background are non-intersecting or merged) for index, (_medium, _shape) in enumerate(background_shapes): - _shape = Box.evaluate_inf_shape(_shape) - # if not intersection, move onto next background shape if not shape & _shape: continue diff --git a/tidy3d/components/viz.py b/tidy3d/components/viz.py index 16450a972b..a4a9c20bf7 100644 --- a/tidy3d/components/viz.py +++ b/tidy3d/components/viz.py @@ -168,14 +168,16 @@ class StructEpsParams(PatchParamSwitcher): eps: float eps_max: float eps_min: float + reverse: bool = True def get_plot_params(self) -> PatchParams: """Returns :class:`PatchParams` based on user-supplied args.""" eps_min = min(self.eps_min, 1) delta_eps = self.eps - eps_min delta_eps_max = self.eps_max - eps_min + 1e-5 - eps_fraction = delta_eps / delta_eps_max - color = 1 - eps_fraction + color = delta_eps / delta_eps_max + if self.reverse: + color = 1 - color if self.eps == pec_val: return PatchParams(facecolor="gold", edgecolor="k", lw=1) return PatchParams(facecolor=str(color), edgecolor=str(color), lw=0)