
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/axes_grid1/demo_colorbar_with_inset_locator.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

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

        :ref:`Go to the end <sphx_glr_download_gallery_axes_grid1_demo_colorbar_with_inset_locator.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_axes_grid1_demo_colorbar_with_inset_locator.py:


.. _demo-colorbar-with-inset-locator:

===========================================================
Control the position and size of a colorbar with Inset Axes
===========================================================

This example shows how to control the position, height, and width of colorbars
using `~mpl_toolkits.axes_grid1.inset_locator.inset_axes`.

Inset Axes placement is controlled as for legends: either by providing a *loc*
option ("upper right", "best", ...), or by providing a locator with respect to
the parent bbox.  Parameters such as *bbox_to_anchor* and *borderpad* likewise
work in the same way, and are also demonstrated here.

Users should consider using `.Axes.inset_axes` instead (see
:ref:`colorbar_placement`).

.. redirect-from:: /gallery/axes_grid1/demo_colorbar_of_inset_axes

.. GENERATED FROM PYTHON SOURCE LINES 21-51

.. code-block:: Python


    import matplotlib.pyplot as plt

    from mpl_toolkits.axes_grid1.inset_locator import inset_axes

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])

    im1 = ax1.imshow([[1, 2], [2, 3]])
    axins1 = inset_axes(
        ax1,
        width="50%",  # width: 50% of parent_bbox width
        height="5%",  # height: 5%
        loc="upper right",
    )
    axins1.xaxis.set_ticks_position("bottom")
    fig.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])

    im = ax2.imshow([[1, 2], [2, 3]])
    axins = inset_axes(
        ax2,
        width="5%",  # width: 5% of parent_bbox width
        height="50%",  # height: 50%
        loc="lower left",
        bbox_to_anchor=(1.05, 0., 1, 1),
        bbox_transform=ax2.transAxes,
        borderpad=0,
    )
    fig.colorbar(im, cax=axins, ticks=[1, 2, 3])

    plt.show()


.. _sphx_glr_download_gallery_axes_grid1_demo_colorbar_with_inset_locator.py:

.. only:: html

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

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: demo_colorbar_with_inset_locator.zip <demo_colorbar_with_inset_locator.zip>`


.. only:: html

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

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
