【问题标题】:Python Matplotlib ButtonsPython Matplotlib 按钮
【发布时间】:2015-07-04 04:33:55
【问题描述】:

我是一个可怜的程序员,所以请原谅我的简单问题。 我正在尝试构建一个从串行接口读取数据并将其显示在屏幕上的小程序。我已经能够在 iPython notebook 和 matplotlib 中做到这一点,并且我已经能够在屏幕上添加按钮来控制进入界面的数据请求: 按钮点击 -> ser.write、ser.read、draw

我现在正在努力设计程序,以便按下按钮将以固定的时间步长开始重复数据收集,直到再次关闭按钮。有人可以帮我画出这样一个程序的草图吗?

到目前为止:

%pylab 

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Button

import serial
import binascii
import struct
from time import sleep

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.axis([0, 10, 0, 255])

ser = serial.Serial('COM5', 1000000, timeout=0) 
ser.write("0".encode())
sleep(0.1)
if ser.read()== b'\xf1':
    print ("Interface is responding")
else:
    print ("Interface is NOT responding")
    ser.flush()
    ser.close()
    exit(1)

t = np.linspace(0, 10, 2048)
line1, line2 = plt.plot(t,0*t, t,0*t, lw=2)

def ShowChannel(Channel):
    if Channel==1:
        ser.write("11".encode())
    elif Channel==2:
        ser.write("12".encode())
    sleep(0.05)
    Header = ser.read().decode("utf-8")
    # print(Header)
    if Header == "1":
        data = ser.read(2048)
        y = struct.unpack('2048B', data)
        # print(y)
        if Channel==1:
            line1.set_ydata(y)
        elif Channel==2:
            line2.set_ydata(y)
        fig.canvas.draw()

def one(event):
    ShowChannel(1)

def two(event):
    ShowChannel(2)

axone = plt.axes([0.1, 0.05, 0.1, 0.075])
axtwo = plt.axes([0.21, 0.05, 0.1, 0.075])
axstart = plt.axes([0.40, 0.05, 0.1, 0.075])
axstop = plt.axes([0.51, 0.05, 0.1, 0.075])

bone = Button(axone, '1')
bone.on_clicked(one)
btwo = Button(axtwo, '2')
btwo.on_clicked(two)

按照 cmets 中引用的示例,我添加了以下内容

# Build check button axes
rax = plt.axes([0.7, 0.05, 0.1, 0.1], aspect='equal')
labels = ('go!',)
check = CheckButtons(rax, labels, (False, ))

KeepShowing = False

def func(event):
    global KeepShowing
    KeepShowing = not KeepShowing
#    print(event, KeepShowing)

check.on_clicked(func)

while True:
    if KeepShowing:
        ShowChannel(1)
    sleep(1)

但是底部的循环不是怎么做的。当我用它启动程序时,图形窗口打开,但不显示任何内容。只有当我在 ipython 中中断内核时,屏幕才会生成。

【问题讨论】:

  • 您能否编辑您的问题以包含您目前编写的代码?
  • 我期待一个while 循环——在散文中,“当按钮状态打开时,(阅读、绘图、等待几秒钟,重复)”。按下按钮将在 ON 和 OFF 之间切换按钮状态。 (或使用单选按钮等本质上是开/关之一。)
  • @cphlewis 好吧,我试过了,但没有用。我不知何故不明白回调的概念。当我将程序发送到循环时,整个事情都冻结了,不想再听按钮了。当我手动中断循环时,按钮可以再次使用。
  • checked_buttons 示例甚至不使用回调(显式)。

标签: python button matplotlib


【解决方案1】:

如果要调用例行读取数据的函数,可以使用matplotlib模块中的定时器。代码如下:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
timer = fig.canvas.new_timer(interval)
timer.add_callback(function, args)

时间间隔单位为ms,可以使用timer.start()timer.stop()方法开启或关闭定时器。


根据您的代码,我为每个按钮添加计时器并添加变量以检查计时器是否正在运行:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Button

import serial
import binascii
import struct
from time import sleep

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.axis([0, 10, 0, 255])

ser = serial.Serial('COM5', 1000000, timeout=0) 
ser.write("0".encode())
sleep(0.1)
if ser.read()== b'\xf1':
    print ("Interface is responding")
else:
    print ("Interface is NOT responding")
    ser.flush()
    ser.close()
    exit(1)

t = np.linspace(0, 10, 2048)
line1, line2 = plt.plot(t,0*t, t,0*t, lw=2)

def ShowChannel(Channel):
    if Channel==1:
        ser.write("11".encode())
    elif Channel==2:
        ser.write("12".encode())
    sleep(0.05)
    Header = ser.read().decode("utf-8")
    # print(Header)
    if Header == "1":
        data = ser.read(2048)
        y = struct.unpack('2048B', data)
        # print(y)
        if Channel==1:
            line1.set_ydata(y)
        elif Channel==2:
            line2.set_ydata(y)
        fig.canvas.draw()

def one(event):
    global channel1_on

    if channel1_on == 0:
        channel1_on = 1
        timer1.start()
    else:
        channel1_on = 0
        timer1.stop()
        line1.set_ydata(None)

def two(event):
    global channel2_on

    if channel2_on == 0:
        channel2_on = 1
        timer2.start()
    else:
        channel2_on = 0
        timer2.stop()
        line2.set_ydata(None)

channel1_on = 0
channel2_on = 0
timer1 = fig.canvas.new_timer(interval = 50)
timer1.add_callback(ShowChannel, 1)
timer2 = fig.canvas.new_timer(interval = 50)
timer2.add_callback(ShowChannel, 2)

axone = plt.axes([0.1, 0.05, 0.1, 0.075])
axtwo = plt.axes([0.21, 0.05, 0.1, 0.075])
axstart = plt.axes([0.40, 0.05, 0.1, 0.075])
axstop = plt.axes([0.51, 0.05, 0.1, 0.075])

bone = Button(axone, '1')
bone.on_clicked(one)
btwo = Button(axtwo, '2')
btwo.on_clicked(two)

plt.show()

另一件事是,如果我没有在您的代码中添加行plt.show(),它在我的计算机中运行时没有显示该数字。所以我将它添加到您的代码末尾,它现在可以显示数字了。

希望对你有帮助。

【讨论】:

  • 是的,这个计时器可以做到!谢谢 1m
猜你喜欢
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多