{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Axes zoom effect\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom matplotlib.transforms import Bbox, TransformedBbox, blended_transform_factory\nfrom mpl_toolkits.axes_grid1.inset_locator import (BboxConnector, BboxConnectorPatch,\n                                                   BboxPatch)\n\n\ndef connect_bbox(bbox1, bbox2,\n                 loc1a, loc2a, loc1b, loc2b,\n                 prop_lines, prop_patches=None):\n    if prop_patches is None:\n        prop_patches = {\n            **prop_lines,\n            \"alpha\": prop_lines.get(\"alpha\", 1) * 0.2,\n            \"clip_on\": False,\n        }\n\n    c1 = BboxConnector(\n        bbox1, bbox2, loc1=loc1a, loc2=loc2a, clip_on=False, **prop_lines)\n    c2 = BboxConnector(\n        bbox1, bbox2, loc1=loc1b, loc2=loc2b, clip_on=False, **prop_lines)\n\n    bbox_patch1 = BboxPatch(bbox1, **prop_patches)\n    bbox_patch2 = BboxPatch(bbox2, **prop_patches)\n\n    p = BboxConnectorPatch(bbox1, bbox2,\n                           loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b,\n                           clip_on=False,\n                           **prop_patches)\n\n    return c1, c2, bbox_patch1, bbox_patch2, p\n\n\ndef zoom_effect01(ax1, ax2, xmin, xmax, **kwargs):\n    \"\"\"\n    Connect *ax1* and *ax2*. The *xmin*-to-*xmax* range in both Axes will\n    be marked.\n\n    Parameters\n    ----------\n    ax1\n        The main Axes.\n    ax2\n        The zoomed Axes.\n    xmin, xmax\n        The limits of the colored area in both plot Axes.\n    **kwargs\n        Arguments passed to the patch constructor.\n    \"\"\"\n\n    bbox = Bbox.from_extents(xmin, 0, xmax, 1)\n\n    mybbox1 = TransformedBbox(bbox, ax1.get_xaxis_transform())\n    mybbox2 = TransformedBbox(bbox, ax2.get_xaxis_transform())\n\n    prop_patches = {**kwargs, \"ec\": \"none\", \"alpha\": 0.2}\n\n    c1, c2, bbox_patch1, bbox_patch2, p = connect_bbox(\n        mybbox1, mybbox2,\n        loc1a=3, loc2a=2, loc1b=4, loc2b=1,\n        prop_lines=kwargs, prop_patches=prop_patches)\n\n    ax1.add_patch(bbox_patch1)\n    ax2.add_patch(bbox_patch2)\n    ax2.add_patch(c1)\n    ax2.add_patch(c2)\n    ax2.add_patch(p)\n\n    return c1, c2, bbox_patch1, bbox_patch2, p\n\n\ndef zoom_effect02(ax1, ax2, **kwargs):\n    \"\"\"\n    ax1 : the main Axes\n    ax1 : the zoomed Axes\n\n    Similar to zoom_effect01.  The xmin & xmax will be taken from the\n    ax1.viewLim.\n    \"\"\"\n\n    tt = ax1.transScale + (ax1.transLimits + ax2.transAxes)\n    trans = blended_transform_factory(ax2.transData, tt)\n\n    mybbox1 = ax1.bbox\n    mybbox2 = TransformedBbox(ax1.viewLim, trans)\n\n    prop_patches = {**kwargs, \"ec\": \"none\", \"alpha\": 0.2}\n\n    c1, c2, bbox_patch1, bbox_patch2, p = connect_bbox(\n        mybbox1, mybbox2,\n        loc1a=3, loc2a=2, loc1b=4, loc2b=1,\n        prop_lines=kwargs, prop_patches=prop_patches)\n\n    ax1.add_patch(bbox_patch1)\n    ax2.add_patch(bbox_patch2)\n    ax2.add_patch(c1)\n    ax2.add_patch(c2)\n    ax2.add_patch(p)\n\n    return c1, c2, bbox_patch1, bbox_patch2, p\n\n\naxs = plt.figure().subplot_mosaic([\n    [\"zoom1\", \"zoom2\"],\n    [\"main\", \"main\"],\n])\n\naxs[\"main\"].set(xlim=(0, 5))\nzoom_effect01(axs[\"zoom1\"], axs[\"main\"], 0.2, 0.8)\naxs[\"zoom2\"].set(xlim=(2, 3))\nzoom_effect02(axs[\"zoom2\"], axs[\"main\"])\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags::\n\n   component: subplot\n   component: transform\n   level: advanced\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}