基于鼠标点击事件的动画颜色网格
Animated color grid based on mouse click event
我想创建一个颜色网格,它接受一些原始用户输入,例如:定义的脚本 运行 时间、随机信号的数量、样本的数量以及归一化 0 或 1。按下鼠标时,其中一列或框应更改为红色,表示 "HIGH",而另一个 boxes/colums 保持蓝色、黄色或绿色。
到目前为止,我有以下代码,但除了能够显示示例颜色网格之外,我没有取得任何进展。我可以采取哪些步骤来执行原始输入并生成随机信号?
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))
使用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))
我想创建一个颜色网格,它接受一些原始用户输入,例如:定义的脚本 运行 时间、随机信号的数量、样本的数量以及归一化 0 或 1。按下鼠标时,其中一列或框应更改为红色,表示 "HIGH",而另一个 boxes/colums 保持蓝色、黄色或绿色。
到目前为止,我有以下代码,但除了能够显示示例颜色网格之外,我没有取得任何进展。我可以采取哪些步骤来执行原始输入并生成随机信号?
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))
使用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))