Python:GUI - 绘图,从实时GUI中读取像素

时间:2018-03-04 23:44:47

标签: python numpy user-interface matplotlib arduino

我有一个项目正在进行中。我是一名新手,室友是一名软件工程师,并建议我在这个项目中使用python。我的问题列在下面。首先,这里是我试图完成的概述。

项目概述:

  

一组可寻址的RGB led矩阵,比如50 leds x 50 leds(250   发光二极管)。 led矩阵连接到arduino并由其运行   将从分散的方式接收矩阵的模式信息   程序。 (我们稍后会担心arduino的功能)

     

该程序的目的是生成和发送模式信息   对于arduino的每个可寻址LED。

     

该程序将托管一个GUI,以便改变和可视化   实时输出或当前矩阵色彩图和图案(即打开/关闭   频闪效果,打开/关闭淡入淡出效果)。然后该程序将阅读   从gui生成并转换RGB值发送到   Arduino的。

这是我所处的地方,我需要指导。截至目前,我正专注于让GUI正常工作,然后再进入该项目的下一部分。

我正在使用matplotlib,希望我可以创建一个50x50正方形(或像素)的图,并保持对每个个体点的价值的控制并大大挣扎。理想情况下,我可以每秒绘制30次绘图,或者多次绘制,以便它可以“实时”更新。

以下是一些示例代码,以便您更好地了解我要完成的任务:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import cm
from numpy.random import random

fig = plt.figure()
matrix = random((50,50))
plt.imshow(matrix, interpolation='nearest', cmap=cm.spectral)


def update(data):
    print("IN UPDATE LOOP")
    matrix = random((50,50))
    return matrix

def data_gen():
    print("IN DATA_GEN LOOP")
    while True: yield np.random.rand(10)


ani = animation.FuncAnimation(fig, update, data_gen, interval=1000)
plt.imshow(matrix, interpolation='nearest', cmap=cm.spectral)
plt.show()
plt.draw()

Photo of matrix with random values assigned to each square

网格不会更新,不知道为什么......

为什么我的网格没有更新?

1 个答案:

答案 0 :(得分:1)

忽略前两个问题,因为它们不是真正的主题,代码的问题是你从未真正更新图像。这应该在动画功能中完成。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import cm
from numpy.random import random

fig = plt.figure()
matrix = random((50,50))
im = plt.imshow(matrix, interpolation='nearest', cmap=cm.Spectral)

def update(data):
    im.set_array(data)

def data_gen():
    while True: 
        yield random((50,50))

ani = animation.FuncAnimation(fig, update, data_gen, interval=1000)

plt.show()
相关问题