1

This should be really simple but for some reason I can't get it to work:

def plot_image(images, heatmaps):
    plt.figure(0)
    for i, (image, map) in enumerate(zip(images, heatmaps)):
        a = plt.subplot2grid((2,4), (0,i))
        a.imshow(image)
        a = plt.subplot2grid((2,4), (1,i))
        a.imshow(map)
        plt.colorbar(a, fraction=0.046, pad=0.04) 
    plt.show()

The values in colorbar line are taken from here, but I'm getting:

AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'

I'm plotting 2 by 4 grid of images, and I'd like to display vertical colorbars to the right from each image, or perhaps only next to the rightmost images in the grid.

MichaelSB
  • 3,131
  • 3
  • 26
  • 40

1 Answers1

3

plt.colorbar expects an image as its first argument (or in general a ScalarMappable), not an axes.

plt.colorbar(im, ax=ax, ...)

Therefore your example should read:

import numpy as np
import matplotlib.pyplot as plt

def plot_image(images, heatmaps):
    fig = plt.figure(0)
    for i, (image, map) in enumerate(zip(images, heatmaps)):
        ax = plt.subplot2grid((2,4), (0,i))
        im = ax.imshow(image)
        ax2 = plt.subplot2grid((2,4), (1,i))
        im2 = ax2.imshow(map)
        fig.colorbar(im, ax=ax, fraction=0.046, pad=0.04) 
        fig.colorbar(im2, ax=ax2, fraction=0.046, pad=0.04) 
    plt.show()

a = [np.random.rand(5,5) for i in range(4)]
b = [np.random.rand(5,5) for i in range(4)]
plot_image(a,b)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712