2017-07-18 57 views
1

這應該是很簡單的,但由於某種原因,我無法得到它的工作:如何添加一個彩條來subplot2grid

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

在彩條線的值是從here拍攝,但我越來越:

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

我被4網格圖像的繪製2,我想從每個圖像顯示垂直colorbars到右邊,或也許僅次於在網格中的最右邊的圖像。

回答

1

plt.colorbar需要一個圖像作爲它的第一個參數(或通常是一個ScalarMappable),而不是一個座標軸。

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

因此你的榜樣應該是:

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