Note

Go to the endto download the full example code.

Axes divider#

Axes divider to calculate location of Axes andcreate a divider for them using existing Axes instances.

demo axes divider
importmatplotlib.pyplotaspltfrommatplotlibimportcbookdefget_demo_image():z=cbook.get_sample_data("axes_grid/bivariate_normal.npy")# 15x15 arrayreturnz,(-3,4,-4,3)defdemo_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)defdemo_locatable_axes_hard(fig):frommpl_toolkits.axes_grid1importSize,SubplotDividerdivider=SubplotDivider(fig,2,2,2,aspect=True)# Axes for imageax=fig.add_subplot(axes_locator=divider.new_locator(nx=0,ny=0))# Axes for colorbarax_cb=fig.add_subplot(axes_locator=divider.new_locator(nx=2,ny=0))divider.set_horizontal([Size.AxesX(ax),# main AxesSize.Fixed(0.05),# padding, 0.1 inchSize.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)defdemo_locatable_axes_easy(ax):frommpl_toolkits.axes_grid1importmake_axes_locatabledivider=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)defdemo_images_side_by_side(ax):frommpl_toolkits.axes_grid1importmake_axes_locatabledivider=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)defdemo():fig=plt.figure(figsize=(6,6))# PLOT 1# simple image & colorbarax=fig.add_subplot(2,2,1)demo_simple_image(ax)# PLOT 2# image and colorbar with draw-time positioning -- a hard waydemo_locatable_axes_hard(fig)# PLOT 3# image and colorbar with draw-time positioning -- an easy wayax=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()

Gallery generated by Sphinx-Gallery