2010-03-29 126 views
4

我想要顯示錶示圖像的沿側的matplotlib imshow副區,其顯示圖像,歸一化的原始值的彩條。如何設置matplotlib顏色條盤區?

我已經能夠成功地繪製圖像和彩條這樣的,但是彩條最小值和最大值代表的歸一化(0,1)的圖像,而不是原始(0,99)圖像。

f = plt.figure() 
# create toy image 
im = np.ones((100,100)) 
for x in range(100): 
    im[x] = x 
# create imshow subplot 
ax = f.add_subplot(111) 
result = ax.imshow(im/im.max()) 

# Create the colorbar 
axc, kw = matplotlib.colorbar.make_axes(ax) 
cb = matplotlib.colorbar.Colorbar(axc, result) 

# Set the colorbar 
result.colorbar = cb 

如果有人更好地掌握colorbar API,我很樂意聽取您的意見。

謝謝! 亞當

回答

7

看起來你傳遞了錯誤的對象到彩條構造。

這應該工作:

# make namespace explicit 
from matplotlib import pyplot as PLT 

cbar = fig.colorbar(result) 

的片段上方是基於你的答案代碼;這裏是一個完整的,獨立的例子:

import numpy as NP 
from matplotlib import pyplot as PLT 

A = NP.random.random_integers(0, 10, 100).reshape(10, 10) 
fig = PLT.figure() 
ax1 = fig.add_subplot(111) 

cax = ax1.imshow(A, interpolation="nearest") 

# set the tickmarks *if* you want cutom (ie, arbitrary) tick labels: 
cbar = fig.colorbar(cax, ticks=[0, 5, 10]) 

# note: 'ax' is not the same as the 'axis' instance created by calling 'add_subplot' 
# the latter instance i bound to the variable 'ax1' to avoid confusing the two 
cbar.ax.set_yticklabels(["lo", "med", "hi"]) 

PLT.show() 

正如我在上面的評論所說,我會選擇一個清潔命名空間,你有什麼 - 比如,有在這兩個NumPy的同名模塊和Matplotlib。

特別,我會用這個import語句將Matplotlib的「核心」繪圖功能:

from matplotlib import pyplot as PLT 

當然,這並沒有得到整個matplotlib命名空間(這是真的這個import語句點)雖然這通常是你需要的一切。

+0

我的興趣@doug的,你爲什麼要利用PLT和NP? – bdforbes 2013-09-03 09:23:06

+0

@bdforbes:所以我就像是乾淨的(沒有重疊瓦特/其他)命名空間再加上我只是想有一個快速的可視化的方式,以確定他們在我的代碼 - 這裏當然,無論PLT和NP綁定到局部變量包含其他模塊的庫/模塊。 – doug 2013-09-03 17:15:34

1

我知道這可能爲時已晚,但...
對於由ax我的亞當的代碼result最後一行替換工作了。