2017-08-06 67 views
0

我正在創建一個顏色網格,它需要一些原始用戶輸入,如:定義的腳本運行時間,隨機信號數量,樣本數量和歸一化0或1。鼠標,其中一列或框應變爲紅色,表示「高」,而其他框/柱保持藍色,黃色或綠色。基於鼠標點擊事件的動畫顏色網格

到目前爲止,我有以下代碼,但除了能夠顯示示例顏色網格之外,我沒有取得任何進展。我可以採取哪些步驟來執行原始輸入並生成隨機信號?

from pylab import arange, cm, draw, rand 
from matplotlib import pylab as plt 
from time import sleep 
import time 

start_time = time.time() 
plt.ion() 
a = arange(25) 
a = a.reshape(5,5) 
fig = plt.figure(figsize = (5, 5)) 
for i in range(100): 
    ax = fig.add_subplot(111) 
    b = 5*rand(5,5) 
    cax = ax.matshow(a-b, cmap=cm.jet, vmin = -10, vmax = 25) 
    if i == 0: 
     fig.colorbar(cax) 
    draw() 
    sleep(0.01) 
plt.show() 
print("--- %s seconds ---" %(time.time() - start_time)) 

回答

1

使用fig.canvas.mpl_connect('button_press_event', update)設置回調函數(例如update)被稱爲每當用戶點擊鼠標:

import numpy as np 
from matplotlib import pyplot as plt 
import matplotlib.colors as mcolors 
import time 

start_time = time.time() 
def generate_data(): 
    a = np.arange(25).reshape(5, 5) 
    b = 10 * np.random.rand(5, 5) 
    result = a - b 
    n = result.size 
    np.put(result, np.random.randint(n), 25) 
    return result 

def update(event): 
    data = generate_data() 
    mat.set_data(data) 
    fig.canvas.draw() 
    return mat 

fig, ax = plt.subplots() 
mat = ax.matshow(generate_data(), cmap=plt.get_cmap('jet')) 
plt.colorbar(mat) 
fig.canvas.mpl_connect('button_press_event', update) 
plt.show() 
print("--- %s seconds ---" %(time.time() - start_time))