2015-11-16 22 views
-1

如何設置matplotlib顯示數組的每個圖像?如何設置matplotlib顯示數組的每個圖像?

我想那每次我點擊右箭頭,這表明下一個圖像等...

這可能嗎?

width = 14 
height = 14 

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 

data_images = X_train.reshape(X_train.shape[0],width,height) 

print "Shape " ,data_images.shape #Shape (50000L, 14L, 14L) 

plt.imshow(data_images[0]) 
plt.show() 

我想通過「data_images」變量plt.imshow所以每次我點擊旁邊的matplotlib,它會顯示下一個圖像。

+0

肘http://matplotlib.org/1.5.0/examples/pylab_examples/toggle_images.html – furas

+0

但我有50 000圖片。我不能繼續做im2.getVisible等所有的人。 – KenobiShan

+0

最重要的是'plt.connect(...)' – furas

回答

1

工作示例plt.connect()

您可以通過按任意鍵更改圖像。

import matplotlib.pyplot as plt 

data_images = [ 
    [[1,2,3],[1,2,3],[1,2,3]], 
    [[1,1,1],[2,2,2],[3,3,3]], 
    [[1,2,1],[2,2,2],[1,2,1]], 
] 

#---------------------------------- 

index = 0 

def toggle_images(event): 
    global index 

    index += 1 

    if index < len(data_images): 
     plt.imshow(data_images[index]) 
     plt.draw() 
    else:   
     plt.close() 

#---------------------------------- 

plt.imshow(data_images[index]) 

plt.connect('key_press_event', toggle_images) 
plt.show() 
+0

NameError:未定義全局名稱'index' – KenobiShan

+0

我需要將整個代碼放在def中,但是一旦我這樣做,就會出現該錯誤。 – KenobiShan

+0

'index = 0'不能在'def'裏面 - 它必須是全局變量。 – furas

1

我會在IPython筆記本中使用ipywidgets。這裏有一個例子:

%matplotlib inline 
import matplotlib.pyplot as plt 
import numpy as np 
from ipywidgets import interact 

images = np.random.random((500, 14, 14)) 

def browse_images(images): 
    N = images.shape[0] 
    def view_image(i=0): 
     plt.imshow(images[i], cmap='gray', interpolation='nearest') 
     plt.title('Image {0}'.format(i)) 
    interact(view_image, i=(0, N-1)) 

browse_images(images) 

編輯:結果,在筆記本頁面,會是這個樣子:

enter image description here

可以按向左或向右箭頭前進滑塊和查看下一張照片。

+0

這真棒!不過,我用了更簡單的東西!但非常感謝答覆!我會牢記這一點! – KenobiShan

1

你可以在筆記本更好一點比使用內聯:

%matplotlib notebook 
import matplotlib.pyplot as plt 
import numpy as np 
from ipywidgets import interact 
from IPython.display import display 

images = np.random.random((500, 14, 14)) 
fig, ax = plt.subplots() 
im = ax.imshow(images[0], cmap='gray', interpolation='nearest') 
def browse_images(images): 
    N = images.shape[0] 
    def view_image(i=0): 
     im.set_data(images[i]) 
     ax.set_title('Image {0}'.format(i)) 
     fig.canvas.draw_idle() 
    interact(view_image, i=(0, N-1)) 

,然後在下一個單元中

browse_images(images) 

,這將給你一個可平移/縮放能力的身影。在mpl 1.5.0中,默認情況下,您還可以獲得光標下的像素值。

兩個圖像之間通過按 「T」(我測試此上tmpnb.org)

相關問題