def extract_matched_slices(ax, shape):
    ext = (ax.transAxes.transform([(1, 1)]) - ax.transAxes.transform([(0, 0)]))[0]
    xlim, ylim = ax.get_xlim(), ax.get_ylim()
    dx, dy = xlim[1] - xlim[0], ylim[1] - ylim[0]

    def _clip(val, hi):
        return int(max(min(val, hi), 0))

    y0 = _clip(min(ylim) - 5, shape[0])
    y1 = _clip(max(ylim) + 5, shape[0])
    x0 = _clip(min(xlim) - 5, shape[1])
    x1 = _clip(max(xlim) + 5, shape[1])

    sy = int(max(1, min((y1 - y0) / 5., np.ceil(abs(dy / ext[1])))))
    sx = int(max(1, min((x1 - x0) / 5., np.ceil(abs(dx / ext[0])))))

    return x0, x1, sx, y0, y1, sy
Determine the slice parameters to use, matched to the screen.
dataset/ETHPy150Open glue-viz/glue/glue/external/modest_image.py/extract_matched_slices