Skip to content
Related Articles

Related Articles

Set Matplotlib colorbar size to match graph
  • Last Updated : 24 Jan, 2021

Prerequisites: Matplotlib

Colorbar size that match graph or image is required to get good visualize effect. This can be achieved using any one of following approaches.

Method 1: Use fraction parameter

Fraction parameter in colorbar() is used to set the size of colorbar. Using this we can match colorbar size to graph as:

  • If vertical colorbar is used, then fraction=0.047 * (height_of_image / width_of_image)
  • If horizontal colorbar is used, then fraction=0.047 * (width_of_image / height_of_image)

Approach

  • Import module
  • Plot a graph
  • Set fraction parameter
  • Plot colorbar
  • Display plot

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
import numpy as np
  
# Plot an image
a = np.random.random((10, 20))
plt.imshow(a, cmap='gray')
  
# Calculate (height_of_image / width_of_image)
im_ratio = a.shape[0]/a.shape[1]
  
# Plot vertical colorbar
plt.colorbar(fraction=0.047*im_ratio)
plt.show()

chevron_right


Output:

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
import numpy as np
  
# Plot an image
a = np.random.random((10, 20))
plt.imshow(a, cmap='gray')
  
# Calculate (width_of_image/height_of_image)
im_ratio = a.shape[1]/a.shape[0]
  
# Plot horizontal colorbar
plt.colorbar(orientation="horizontal", fraction=0.047*im_ratio)
plt.show()

chevron_right


Output:

Method 2: Using axes_grid1 toolkit

Axis_grid1 provides a helper function make_axes_locatable() which takes an existing axes instance and creates a divider for it. It provides append_axes() method that creates a new axes on the given side (“top”, “right”, “bottom” and “left”) of the original axes. 



Approach:

  • Import module
  • Plot image
  • Divide existing axes instance using make_axes_locatable()
  • Create new axes using append_axes()
    • Use “top” or “bottom” side for horizontal colorbar
    • Use “left” or “right” side for vertical colorbar
  • Plot colorbar on created axis

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
  
# Plot image on axes ax
ax = plt.gca()
img = np.random.random((10, 20))
im = plt.imshow(img, cmap='gray')
  
# Divide existing axes and create new axes
# at bottom side of image
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="5%", pad=0.25)
  
# Plot horizontal colorbar
plt.colorbar(im, orientation="horizontal", cax=cax)
plt.show()

chevron_right


Output:

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
  
# Plot image on axes ax
ax = plt.gca()
img = np.random.random((10, 20))
im = plt.imshow(img, cmap='gray')
  
# Divide existing axes and create
# new axes at right side of image
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.15)
  
# Plot vertical colorbar
plt.colorbar(im, cax=cax)
plt.show()

chevron_right


Output:

Approach 3:  Using add_axes() method

Giving a colorbar its own axes using add_axes() method, can be an approach to get a colorbar matching given image.

Approach:

  • Plot a figure
  • Create axes using add_axes() method with position parameters:
    • For vertical colorbar on right side of the image:
      • left: Left point for colorbar=Right End position of image
      • bottom: Bottom point of colorbar= Bottom end position of image
      • width: Width of colorbar
      • height: Height of colorbar=Height of image
    • For horizontal colorbar at bottom of the image:
      • left: Left point for colorbar=Left End position of image
      • bottom: Bottom point of colorbar= Bottom end position of image
      • width: Width of colorbar =Width of image
      • height: Height of colorbar
  • Plot colorbar on created axes

Example 1 :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
fig = plt.figure()
ax = plt.axes()
img = np.random.random((10, 20))
im = plt.imshow(img, cmap='gray')
  
# Create new axes according to image position
cax = fig.add_axes([ax.get_position().x1+0.01,
                    ax.get_position().y0,
                    0.02,
                    ax.get_position().height])
  
# Plot vertical colorbar
plt.colorbar(im, cax=cax)
plt.show()

chevron_right


Output:

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
import numpy as np
  
# Plot an image
fig = plt.figure()
ax = plt.axes()
img = np.random.random((10, 20))
im = plt.imshow(img, cmap='gray')
  
# Create new axes acoording to image position
cax = fig.add_axes([ax.get_position().x0,
                    ax.get_position().y0-0.08,
                    ax.get_position().width,
                    0.02])
  
# Plot horizontal colorbar on created axes
plt.colorbar(im, orientation="horizontal", cax=cax)
plt.show()

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :