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

问题描述 投票:1回答:1

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

项目概况:

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

该程序的目的是生成每个可寻址LED的模式信息并将其发送到arduino。

该程序将主持一个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

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

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

python numpy user-interface matplotlib arduino
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.