Ignore:
Timestamp:
Jul 7, 2025, 6:09:01 PM (4 days ago)
Author:
jbclement
Message:

PEM:

  • Correction of a bug in the launching script.
  • Update of "visu_evol_layering.py", in particular to show value at cursor for 2D heatmaps.
  • Few cleanings.

JBC

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LMDZ.COMMON/libf/evolution/deftank/visu_evol_layering.py

    r3819 r3840  
    379379
    380380
     381def attach_format_coord(ax, mat, x, y, is_pcolormesh=True):
     382    """
     383    Attach a format_coord function to the axes to display x, y, and value at cursor.
     384    Works for both pcolormesh and imshow style grids.
     385    """
     386    # Determine dimensions
     387    if mat.ndim == 2:
     388        ny, nx = mat.shape
     389    elif mat.ndim == 3 and mat.shape[2] in (3, 4):
     390        ny, nx, nc = mat.shape
     391    else:
     392        raise ValueError(f"Unsupported mat shape {mat.shape}")
     393    # Edges or extents
     394    if is_pcolormesh:
     395        xedges, yedges = x, y
     396    else:
     397        x0, x1 = x.min(), x.max()
     398        y0, y1 = y.min(), y.max()
     399
     400    def format_coord(xp, yp):
     401        # Map to indices
     402        if is_pcolormesh:
     403            col = np.searchsorted(xedges, xp) - 1
     404            row = np.searchsorted(yedges, yp) - 1
     405        else:
     406            col = int((xp - x0) / (x1 - x0) * nx)
     407            row = int((yp - y0) / (y1 - y0) * ny)
     408        # Within bounds?
     409        if 0 <= row < ny and 0 <= col < nx:
     410            if mat.ndim == 2:
     411                v = mat[row, col]
     412                return f"x={xp:.3g}, y={yp:.3g}, val={v:.3g}"
     413            else:
     414                vals = mat[row, col]
     415                txt = ", ".join(f"{vv:.3g}" for vv in vals[:3])
     416                return f"x={xp:.3g}, y={yp:.3g}, val=({txt})"
     417        return f"x={xp:.3g}, y={yp:.3g}"
     418
     419    ax.format_coord = format_coord
     420
     421
    381422def plot_stratification_over_time(
    382423    gridded_data,
     
    454495                    vmax=vmax
    455496                )
     497                x_edges = np.concatenate([date_time, [date_time[-1] + (date_time[-1]-date_time[-2])]])
     498                attach_format_coord(ax, mat, x_edges, np.concatenate([sub_ref_grid, [sub_ref_grid[-1] + (sub_ref_grid[-1]-sub_ref_grid[-2])]]), is_pcolormesh=True)
    456499                ax.set_title(titles[idx], fontsize=12)
    457500                ax.set_xlabel("Time (Mars years)")
     
    558601                origin='lower'
    559602            )
     603            x_centers = np.linspace(date_time[0], date_time[-1], display_rgb.shape[1])
     604            y_centers = np.linspace(elev.min(), elev.max(), display_rgb.shape[0])
     605            attach_format_coord(ax_main, display_rgb, x_centers, y_centers, is_pcolormesh=False)
    560606            ax_main.set_facecolor('white')
    561607            ax_main.set_title(f"Ternary mix over time (Grid point {ig+1}, Slope {isl+1})", fontweight='bold')
     
    570616                interpolation='nearest'
    571617            )
     618            attach_format_coord(ax_leg, legend_rgba, np.array([0, 1]), np.array([0, np.sqrt(3)/2]), is_pcolormesh=False)
    572619
    573620            # Draw triangle border
     
    663710            # Plot
    664711            fig, ax = plt.subplots(figsize=(8, 6), dpi=150)
     712            ax.set_title(f"Dust-to-Ice ratio over time (Grid point {ig+1}, Slope {isl+1})", fontweight='bold')
    665713            im = ax.imshow(
    666714                ratio_display,
     
    672720                norm=LogNorm(vmin=10**vmin, vmax=10**vmax)
    673721            )
     722            x_centers = np.linspace(date_time[0], date_time[-1], ratio_display.shape[1])
     723            y_centers = np.linspace(elev.min(), elev.max(), ratio_display.shape[0])
     724            attach_format_coord(ax, ratio_display, x_centers, y_centers, is_pcolormesh=False)
    674725
    675726            # Add colorbar with simplified ratio labels
Note: See TracChangeset for help on using the changeset viewer.