{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Inset locator demo 2\n\nThis demo shows how to create a zoomed inset via `.zoomed_inset_axes`.\nIn the first subplot an `.AnchoredSizeBar` shows the zoom effect.\nIn the second subplot a connection to the region of interest is\ncreated via `.mark_inset`.\n\nA version of the second subplot, not using the toolkit, is available in\n:doc:`/gallery/subplots_axes_and_figures/zoom_inset_axes`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import cbook\nfrom mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar\nfrom mpl_toolkits.axes_grid1.inset_locator import mark_inset, zoomed_inset_axes\n\nfig, (ax, ax2) = plt.subplots(ncols=2, figsize=[6, 3])\n\n\n# First subplot, showing an inset with a size bar.\nax.set_aspect(1)\n\naxins = zoomed_inset_axes(ax, zoom=0.5, loc='upper right')\n# fix the number of ticks on the inset Axes\naxins.yaxis.get_major_locator().set_params(nbins=7)\naxins.xaxis.get_major_locator().set_params(nbins=7)\naxins.tick_params(labelleft=False, labelbottom=False)\n\n\ndef add_sizebar(ax, size):\n asb = AnchoredSizeBar(ax.transData,\n size,\n str(size),\n loc=\"lower center\",\n pad=0.1, borderpad=0.5, sep=5,\n frameon=False)\n ax.add_artist(asb)\n\nadd_sizebar(ax, 0.5)\nadd_sizebar(axins, 0.5)\n\n\n# Second subplot, showing an image with an inset zoom and a marked inset\nZ = cbook.get_sample_data(\"axes_grid/bivariate_normal.npy\") # 15x15 array\nextent = (-3, 4, -4, 3)\nZ2 = np.zeros((150, 150))\nny, nx = Z.shape\nZ2[30:30+ny, 30:30+nx] = Z\n\nax2.imshow(Z2, extent=extent, origin=\"lower\")\n\naxins2 = zoomed_inset_axes(ax2, zoom=6, loc=\"upper right\")\naxins2.imshow(Z2, extent=extent, origin=\"lower\")\n\n# subregion of the original image\nx1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9\naxins2.set_xlim(x1, x2)\naxins2.set_ylim(y1, y2)\n# fix the number of ticks on the inset Axes\naxins2.yaxis.get_major_locator().set_params(nbins=7)\naxins2.xaxis.get_major_locator().set_params(nbins=7)\naxins2.tick_params(labelleft=False, labelbottom=False)\n\n# draw a bbox of the region of the inset Axes in the parent Axes and\n# connecting lines between the bbox and the inset Axes area\nmark_inset(ax2, axins2, loc1=2, loc2=4, fc=\"none\", ec=\"0.5\")\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }