Exemple de comment ajuster la taille de la colorbar à la figure avec matplotlib :
Colorbar de meme taille que la figure avec matplotlib
Un problème récurrent avec matplotlib est de vouloir adapter la taille de la colorbar au graphique comme dans l'exemple ci-dessous:

import matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import make_axes_locatableimport numpy as npfig = plt.figure(1, figsize=(5, 3))im = plt.imshow(np.arange(200).reshape((10,20)))plt.colorbar(im)plt.savefig('AdaptColorbar1.png')plt.show()
Pour remédier à ce problème il faut passer par le toolkit AxesGrid comme ceci:

import matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import make_axes_locatableimport numpy as npfig = plt.figure(1, figsize=(5, 3))ax = plt.gca()im = ax.imshow(np.arange(200).reshape((10,20)))divider = make_axes_locatable(ax)cax = divider.append_axes("right", size="5%", pad=0.1)plt.colorbar(im, cax=cax)plt.savefig('AdaptColorbar2.png')plt.show()
Colorbar en dessous et de même taille que la figure avec matplotlib
Autre exemple avec la colorbar en dessous:

#!/usr/bin/env pythonimport matplotlib.pyplot as pltimport numpy as np#----------------------------------------------------------------------------------------#def f(x,y):return (x+y)*np.exp(-5.0*(x**2+y**2))x,y = np.mgrid[-1:1:100j, -1:1:100j]z = f(x,y)#----------------------------------------------------------------------------------------#plt.imshow(z,extent=[-1,1,-1,1])plt.colorbar(orientation="horizontal")plt.title("Colorbar bottom position \n with matplotib")plt.savefig('colorbar_positioning_01.png', format='png', bbox_inches='tight')plt.show()plt.close()

#!/usr/bin/env pythonfrom mpl_toolkits.axes_grid1 import make_axes_locatableimport matplotlib.pyplot as pltimport numpy as np#----------------------------------------------------------------------------------------#def f(x,y):return (x+y)*np.exp(-5.0*(x**2+y**2))x,y = np.mgrid[-1:1:100j, -1:1:100j]z = f(x,y)#----------------------------------------------------------------------------------------#fig, ax = plt.subplots(figsize=(4,4))plt.title("Colorbar bottom position \n with matplotib")im = plt.imshow(z,extent=[-1,1,-1,1])divider = make_axes_locatable(ax)cax = divider.new_vertical(size="5%", pad=0.5, pack_start=True)fig.add_axes(cax)fig.colorbar(im, cax=cax, orientation="horizontal")plt.savefig('colorbar_positioning_03.png', format='png', bbox_inches='tight')plt.show()plt.close()
Changer l'aspect ratio et avoir la colorbar de même taille
Exemple plus compliqué en changeant la forme ('aspect ratio') de la figure:

import numpy as npimport matplotlib.pyplot as pltdata = np.random.rand(50,1000)def forceAspect(ax,aspect):im = ax.get_images()extent = im[0].get_extent()ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)fig = plt.figure()ax = fig.add_subplot(111)img = plt.imshow(data, extent=[-1,1,-10,10])forceAspect(ax,aspect=1.0)v1 = np.linspace(data.min(), data.max(), 8, endpoint=True)cb = plt.colorbar(ticks=v1)cb.ax.set_yticklabels(["{:4.2f}".format(i) for i in v1], fontsize='7')fig.savefig("imshow_extent_custum_aspect_ratio_04.png", bbox_inches='tight')
Références
| Liens | Site |
|---|---|
| Overview of AxesGrid toolkit | Matplotlib doc |
| Set Matplotlib colorbar size to match graph | stackoverflow |
