.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/axes_grid1/demo_axes_divider.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_axes_grid1_demo_axes_divider.py: ============ Axes divider ============ Axes divider to calculate location of axes and create a divider for them using existing axes instances. .. GENERATED FROM PYTHON SOURCE LINES 9-110 .. image-sg:: /gallery/axes_grid1/images/sphx_glr_demo_axes_divider_001.png :alt: demo axes divider :srcset: /gallery/axes_grid1/images/sphx_glr_demo_axes_divider_001.png, /gallery/axes_grid1/images/sphx_glr_demo_axes_divider_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt from matplotlib import cbook def get_demo_image(): z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array return z, (-3, 4, -4, 3) def demo_simple_image(ax): Z, extent = get_demo_image() im = ax.imshow(Z, extent=extent) cb = plt.colorbar(im) cb.ax.yaxis.set_tick_params(labelright=False) def demo_locatable_axes_hard(fig): from mpl_toolkits.axes_grid1 import Size, SubplotDivider divider = SubplotDivider(fig, 2, 2, 2, aspect=True) # axes for image ax = fig.add_subplot(axes_locator=divider.new_locator(nx=0, ny=0)) # axes for colorbar ax_cb = fig.add_subplot(axes_locator=divider.new_locator(nx=2, ny=0)) divider.set_horizontal([ Size.AxesX(ax), # main axes Size.Fixed(0.05), # padding, 0.1 inch Size.Fixed(0.2), # colorbar, 0.3 inch ]) divider.set_vertical([Size.AxesY(ax)]) Z, extent = get_demo_image() im = ax.imshow(Z, extent=extent) plt.colorbar(im, cax=ax_cb) ax_cb.yaxis.set_tick_params(labelright=False) def demo_locatable_axes_easy(ax): from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(ax) ax_cb = divider.append_axes("right", size="5%", pad=0.05) fig = ax.get_figure() fig.add_axes(ax_cb) Z, extent = get_demo_image() im = ax.imshow(Z, extent=extent) plt.colorbar(im, cax=ax_cb) ax_cb.yaxis.tick_right() ax_cb.yaxis.set_tick_params(labelright=False) def demo_images_side_by_side(ax): from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(ax) Z, extent = get_demo_image() ax2 = divider.append_axes("right", size="100%", pad=0.05) fig1 = ax.get_figure() fig1.add_axes(ax2) ax.imshow(Z, extent=extent) ax2.imshow(Z, extent=extent) ax2.yaxis.set_tick_params(labelleft=False) def demo(): fig = plt.figure(figsize=(6, 6)) # PLOT 1 # simple image & colorbar ax = fig.add_subplot(2, 2, 1) demo_simple_image(ax) # PLOT 2 # image and colorbar with draw-time positioning -- a hard way demo_locatable_axes_hard(fig) # PLOT 3 # image and colorbar with draw-time positioning -- an easy way ax = fig.add_subplot(2, 2, 3) demo_locatable_axes_easy(ax) # PLOT 4 # two images side by side with fixed padding. ax = fig.add_subplot(2, 2, 4) demo_images_side_by_side(ax) plt.show() demo() .. _sphx_glr_download_gallery_axes_grid1_demo_axes_divider.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_axes_divider.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_axes_divider.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_