|
90 | 90 | MAX_STRUCTURES_PER_MEDIUM = 1_000 |
91 | 91 |
|
92 | 92 |
|
| 93 | +def _get_colormap(reverse: bool = False): |
| 94 | + return STRUCTURE_EPS_CMAP_R if reverse else STRUCTURE_EPS_CMAP |
| 95 | + |
| 96 | + |
93 | 97 | class Scene(Tidy3dBaseModel): |
94 | 98 | """Contains generic information about the geometry and medium properties common to all types of |
95 | 99 | simulations. |
@@ -1200,7 +1204,7 @@ def _add_cbar_eps( |
1200 | 1204 | vmin=eps_min, |
1201 | 1205 | vmax=eps_max, |
1202 | 1206 | label=r"$\epsilon_r$", |
1203 | | - cmap=STRUCTURE_EPS_CMAP if not reverse else STRUCTURE_EPS_CMAP_R, |
| 1207 | + cmap=_get_colormap(reverse=reverse), |
1204 | 1208 | ax=ax, |
1205 | 1209 | norm=norm, |
1206 | 1210 | ) |
@@ -1314,16 +1318,14 @@ def _pcolormesh_shape_custom_medium_structure_eps( |
1314 | 1318 | # extract slice if volumetric unstructured data |
1315 | 1319 | eps = eps.plane_slice(axis=normal_axis_ind, pos=normal_position) |
1316 | 1320 |
|
1317 | | - if reverse: |
1318 | | - eps = eps_min + eps_max - eps |
1319 | | - |
1320 | 1321 | # at this point eps_mean is TriangularGridDataset and we just plot it directly |
1321 | 1322 | # with applying shape mask |
| 1323 | + cmap_name = _get_colormap(reverse=reverse) |
1322 | 1324 | eps.plot( |
1323 | 1325 | grid=False, |
1324 | 1326 | ax=ax, |
1325 | 1327 | cbar=False, |
1326 | | - cmap=STRUCTURE_EPS_CMAP, |
| 1328 | + cmap=cmap_name, |
1327 | 1329 | vmin=eps_min, |
1328 | 1330 | vmax=eps_max, |
1329 | 1331 | pcolor_kwargs={ |
@@ -1395,18 +1397,15 @@ def _pcolormesh_shape_custom_medium_structure_eps( |
1395 | 1397 |
|
1396 | 1398 | # remove the normal_axis and take real part |
1397 | 1399 | eps_shape = eps_shape.real.mean(axis=normal_axis_ind) |
1398 | | - # reverse |
1399 | | - if reverse: |
1400 | | - eps_shape = eps_min + eps_max - eps_shape |
1401 | | - |
1402 | 1400 | # pcolormesh |
1403 | 1401 | plane_xp, plane_yp = np.meshgrid(plane_coord[0], plane_coord[1], indexing="ij") |
| 1402 | + cmap_name = _get_colormap(reverse=reverse) |
1404 | 1403 | ax.pcolormesh( |
1405 | 1404 | plane_xp, |
1406 | 1405 | plane_yp, |
1407 | 1406 | eps_shape, |
1408 | 1407 | clip_path=(polygon_path(shape), ax.transData), |
1409 | | - cmap=STRUCTURE_EPS_CMAP, |
| 1408 | + cmap=cmap_name, |
1410 | 1409 | alpha=alpha, |
1411 | 1410 | clip_box=ax.bbox, |
1412 | 1411 | norm=norm, |
@@ -1447,23 +1446,15 @@ def _get_structure_eps_plot_params( |
1447 | 1446 | plot_params = plot_params.copy(update={"edgecolor": "k", "linewidth": 1}) |
1448 | 1447 | else: |
1449 | 1448 | eps_medium = medium._eps_plot(frequency=freq, eps_component=eps_component) |
1450 | | - if norm is not None: |
1451 | | - # Use the same normalization as the colorbar for consistency |
1452 | | - color = norm(eps_medium) |
1453 | | - # TODO: This is a hack to ensure color consistency with the colorbar. |
1454 | | - # It should be removed once we establish a proper color mapping where |
1455 | | - # eps_min maps to 0 and eps_max maps to 1 for 'reverse=False'. |
1456 | | - if not reverse: |
1457 | | - color = 1 - color |
1458 | | - color = min(1, max(color, 0)) # clip in case of custom eps limits |
1459 | | - else: |
1460 | | - # Fallback to linear mapping for backward compatibility |
1461 | | - delta_eps = eps_medium - eps_min |
1462 | | - delta_eps_max = eps_max - eps_min + 1e-5 |
1463 | | - eps_fraction = delta_eps / delta_eps_max |
1464 | | - color = eps_fraction if reverse else 1 - eps_fraction |
1465 | | - color = min(1, max(color, 0)) # clip in case of custom eps limits |
1466 | | - plot_params = plot_params.copy(update={"facecolor": str(color)}) |
| 1449 | + active_norm = ( |
| 1450 | + norm if norm is not None else mpl.colors.Normalize(vmin=eps_min, vmax=eps_max) |
| 1451 | + ) |
| 1452 | + color_value = float(active_norm(eps_medium)) |
| 1453 | + color_value = min(1.0, max(0.0, color_value)) |
| 1454 | + cmap_name = _get_colormap(reverse=reverse) |
| 1455 | + cmap = mpl.cm.get_cmap(cmap_name) |
| 1456 | + rgba = tuple(float(component) for component in cmap(color_value)) |
| 1457 | + plot_params = plot_params.copy(update={"facecolor": rgba}) |
1467 | 1458 |
|
1468 | 1459 | return plot_params |
1469 | 1460 |
|
|
0 commit comments