.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_gallery_axes_grid1_inset_locator_demo2.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_axes_grid1_inset_locator_demo2.py:


===================
Inset Locator Demo2
===================

This Demo shows how to create a zoomed inset via `~.zoomed_inset_axes`.
In the first subplot an `~.AnchoredSizeBar` shows the zoom effect.
In the second subplot a connection to the region of interest is
created via `~.mark_inset`.




.. image:: /gallery/axes_grid1/images/sphx_glr_inset_locator_demo2_001.png
    :class: sphx-glr-single-img





.. code-block:: python


    import matplotlib.pyplot as plt

    from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar

    import numpy as np


    def get_demo_image():
        from matplotlib.cbook import get_sample_data
        import numpy as np
        f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
        z = np.load(f)
        # z is a numpy array of 15x15
        return z, (-3, 4, -4, 3)

    fig, (ax, ax2) = plt.subplots(ncols=2, figsize=[6, 3])


    # First subplot, showing an inset with a size bar.
    ax.set_aspect(1)

    axins = zoomed_inset_axes(ax, zoom=0.5, loc='upper right')
    # fix the number of ticks on the inset axes
    axins.yaxis.get_major_locator().set_params(nbins=7)
    axins.xaxis.get_major_locator().set_params(nbins=7)

    plt.setp(axins.get_xticklabels(), visible=False)
    plt.setp(axins.get_yticklabels(), visible=False)


    def add_sizebar(ax, size):
        asb = AnchoredSizeBar(ax.transData,
                              size,
                              str(size),
                              loc=8,
                              pad=0.1, borderpad=0.5, sep=5,
                              frameon=False)
        ax.add_artist(asb)

    add_sizebar(ax, 0.5)
    add_sizebar(axins, 0.5)


    # Second subplot, showing an image with an inset zoom
    # and a marked inset
    Z, extent = get_demo_image()
    Z2 = np.zeros([150, 150], dtype="d")
    ny, nx = Z.shape
    Z2[30:30 + ny, 30:30 + nx] = Z

    # extent = [-3, 4, -4, 3]
    ax2.imshow(Z2, extent=extent, interpolation="nearest",
              origin="lower")


    axins2 = zoomed_inset_axes(ax2, 6, loc=1)  # zoom = 6
    axins2.imshow(Z2, extent=extent, interpolation="nearest",
                  origin="lower")

    # sub region of the original image
    x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
    axins2.set_xlim(x1, x2)
    axins2.set_ylim(y1, y2)
    # fix the number of ticks on the inset axes
    axins2.yaxis.get_major_locator().set_params(nbins=7)
    axins2.xaxis.get_major_locator().set_params(nbins=7)

    plt.setp(axins2.get_xticklabels(), visible=False)
    plt.setp(axins2.get_yticklabels(), visible=False)

    # draw a bbox of the region of the inset axes in the parent axes and
    # connecting lines between the bbox and the inset axes area
    mark_inset(ax2, axins2, loc1=2, loc2=4, fc="none", ec="0.5")

    plt.show()


.. _sphx_glr_download_gallery_axes_grid1_inset_locator_demo2.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: inset_locator_demo2.py <inset_locator_demo2.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: inset_locator_demo2.ipynb <inset_locator_demo2.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
