Open In App

Set Matplotlib colorbar size to match graph

Last Updated : 08 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Use fraction parameter to match graph

Fraction parameter in colorbar() is used to set the size of colorbar in Python. 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:

Here we are importing the required libraries and then plotting the graph, we are calculating the ratio then we are setting the fraction parameter and plotting the colorbar and finally displaying it.

Python3




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


Output:

Set Matplotlib colorbar size to match graph

Set Matplotlib colorbar size to match graph

Example 2:

Python3




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


Output:

Set Matplotlib colorbar size to match graph

Using axes_grid1 toolkit to match graph

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




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


Output:

Set Matplotlib colorbar size to match graph

Example 2:

Python3




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


Output:

Set Matplotlib colorbar size to match graph

Using add_axes() method to match graph

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




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


Output:

Set Matplotlib colorbar size to match graph

Example 2:

Python3




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 according 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()


Output:

Set Matplotlib colorbar size to match graph

Prerequisites: Matplotlib



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads