How to have the colorbar with same size as the figure in matpltolib ?

Published: March 24, 2019

DMCA.com Protection Status

Examples of how to adjust the size of a colorbar with the figure in matplotlib

Colorbar same size as the figure in matplotlib

Example with a simple vertical colorbar:

How to match the colorbar size with the figure size in matpltolib ?
How to match the colorbar size with the figure size in matpltolib ?

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig = plt.figure(1, figsize=(5, 3))

im = plt.imshow(np.arange(200).reshape((10,20)))

plt.colorbar(im)

plt.savefig('AdaptColorbar1.png')
plt.show()

To adjust the colorbar size with the figure, a solution is to use the toolkit AxesGrid:

How to match the colorbar size with the figure size in matpltolib ?
How to match the colorbar size with the figure size in matpltolib ?

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig = 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()

Horizontal colorbar same size as the figure in matplotlib

Another example with an horizontal colorbar:

How to match the colorbar size with the figure size in matpltolib ?
How to match the colorbar size with the figure size in matpltolib ?

#!/usr/bin/env python

import matplotlib.pyplot as plt
import 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()

How to match the colorbar size with the figure size in matpltolib ?
How to match the colorbar size with the figure size in matpltolib ?

#!/usr/bin/env python

    from mpl_toolkits.axes_grid1 import make_axes_locatable

import matplotlib.pyplot as plt
import 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()

Change imshow aspect ratio and keep the colorbar of same size

Another example: changing the imshow aspect ratio

How to match the colorbar size with the figure size in matpltolib ?
How to match the colorbar size with the figure size in matpltolib ?

import numpy as np
import matplotlib.pyplot as plt

data = 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')

References

Image

of