{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Inset locator demo\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The `.inset_locator`'s `~.inset_locator.inset_axes` allows\neasily placing insets in the corners of the Axes by specifying a width and\nheight and optionally a location (loc) that accepts locations as codes,\nsimilar to `~matplotlib.axes.Axes.legend`.\nBy default, the inset is offset by some points from the axes,\ncontrolled via the *borderpad* parameter.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom mpl_toolkits.axes_grid1.inset_locator import inset_axes\n\nfig, (ax, ax2) = plt.subplots(1, 2, figsize=[5.5, 2.8])\n\n# Create inset of width 1.3 inches and height 0.9 inches\n# at the default upper right location.\naxins = inset_axes(ax, width=1.3, height=0.9)\n\n# Create inset of width 30% and height 40% of the parent Axes' bounding box\n# at the lower left corner.\naxins2 = inset_axes(ax, width=\"30%\", height=\"40%\", loc=\"lower left\")\n\n# Create inset of mixed specifications in the second subplot;\n# width is 30% of parent Axes' bounding box and\n# height is 1 inch at the upper left corner.\naxins3 = inset_axes(ax2, width=\"30%\", height=1., loc=\"upper left\")\n\n# Create an inset in the lower right corner with borderpad=1, i.e.\n# 10 points padding (as 10pt is the default fontsize) to the parent Axes.\naxins4 = inset_axes(ax2, width=\"20%\", height=\"20%\", loc=\"lower right\", borderpad=1)\n\n# Turn ticklabels of insets off\nfor axi in [axins, axins2, axins3, axins4]:\n    axi.tick_params(labelleft=False, labelbottom=False)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The parameters *bbox_to_anchor* and *bbox_transform* can be used for a more\nfine-grained control over the inset position and size or even to position\nthe inset at completely arbitrary positions.\nThe *bbox_to_anchor* sets the bounding box in coordinates according to the\n*bbox_transform*.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=[5.5, 2.8])\nax = fig.add_subplot(121)\n\n# We use the Axes transform as bbox_transform. Therefore, the bounding box\n# needs to be specified in axes coordinates ((0, 0) is the lower left corner\n# of the Axes, (1, 1) is the upper right corner).\n# The bounding box (.2, .4, .6, .5) starts at (.2, .4) and ranges to (.8, .9)\n# in those coordinates.\n# Inside this bounding box an inset of half the bounding box' width and\n# three quarters of the bounding box' height is created. The lower left corner\n# of the inset is aligned to the lower left corner of the bounding box.\n# The inset is then offset by the default 0.5 in units of the font size.\n\naxins = inset_axes(ax, width=\"50%\", height=\"75%\",\n                   bbox_to_anchor=(.2, .4, .6, .5),\n                   bbox_transform=ax.transAxes, loc=\"lower left\")\n\n# For visualization purposes we mark the bounding box by a rectangle\nax.add_patch(plt.Rectangle((.2, .4), .6, .5, ls=\"--\", ec=\"c\", fc=\"none\",\n                           transform=ax.transAxes))\n\n# We set the axis limits to something other than the default, in order to not\n# distract from the fact that axes coordinates are used here.\nax.set(xlim=(0, 10), ylim=(0, 10))\n\n\n# Note how the two following insets are created at the same positions, one by\n# use of the default parent Axes' bbox and the other via a bbox in Axes\n# coordinates and the respective transform.\nax2 = fig.add_subplot(222)\naxins2 = inset_axes(ax2, width=\"30%\", height=\"50%\")\n\nax3 = fig.add_subplot(224)\naxins3 = inset_axes(ax3, width=\"100%\", height=\"100%\",\n                    bbox_to_anchor=(.7, .5, .3, .5),\n                    bbox_transform=ax3.transAxes)\n\n# For visualization purposes we mark the bounding box by a rectangle\nax2.add_patch(plt.Rectangle((0, 0), 1, 1, ls=\"--\", lw=2, ec=\"c\", fc=\"none\"))\nax3.add_patch(plt.Rectangle((.7, .5), .3, .5, ls=\"--\", lw=2,\n                            ec=\"c\", fc=\"none\"))\n\n# Turn ticklabels off\nfor axi in [axins2, axins3, ax2, ax3]:\n    axi.tick_params(labelleft=False, labelbottom=False)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In the above the Axes transform together with 4-tuple bounding boxes has been\nused as it mostly is useful to specify an inset relative to the Axes it is\nan inset to. However, other use cases are equally possible. The following\nexample examines some of those.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=[5.5, 2.8])\nax = fig.add_subplot(131)\n\n# Create an inset outside the Axes\naxins = inset_axes(ax, width=\"100%\", height=\"100%\",\n                   bbox_to_anchor=(1.05, .6, .5, .4),\n                   bbox_transform=ax.transAxes, loc=\"upper left\", borderpad=0)\naxins.tick_params(left=False, right=True, labelleft=False, labelright=True)\n\n# Create an inset with a 2-tuple bounding box. Note that this creates a\n# bbox without extent. This hence only makes sense when specifying\n# width and height in absolute units (inches).\naxins2 = inset_axes(ax, width=0.5, height=0.4,\n                    bbox_to_anchor=(0.33, 0.25),\n                    bbox_transform=ax.transAxes, loc=\"lower left\", borderpad=0)\n\n\nax2 = fig.add_subplot(133)\nax2.set_xscale(\"log\")\nax2.set(xlim=(1e-6, 1e6), ylim=(-2, 6))\n\n# Create inset in data coordinates using ax.transData as transform\naxins3 = inset_axes(ax2, width=\"100%\", height=\"100%\",\n                    bbox_to_anchor=(1e-2, 2, 1e3, 3),\n                    bbox_transform=ax2.transData, loc=\"upper left\", borderpad=0)\n\n# Create an inset horizontally centered in figure coordinates and vertically\n# bound to line up with the Axes.\nfrom matplotlib.transforms import blended_transform_factory  # noqa\n\ntransform = blended_transform_factory(fig.transFigure, ax2.transAxes)\naxins4 = inset_axes(ax2, width=\"16%\", height=\"34%\",\n                    bbox_to_anchor=(0, 0, 1, 1),\n                    bbox_transform=transform, loc=\"lower center\", borderpad=0)\n\nplt.show()"
      ]
    }
  ],
  "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
}