Python Matplotlib Buttons

I am a bad programmer, so please excuse my simple question. I am trying to create a small program that reads data from a serial interface and displays it on the screen. I was able to do this in iPython laptop and matplotlib, and I was able to add buttons to the screen, which manages the data requests that go to the interface: Press the button → ser.write, ser.read, draw

Now I'm trying to develop a program in such a way that when the button is pressed, data will be re-collected with fixed time steps until the button is turned off again. Can someone please help me with a sketch for such a program?

Still:

%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)

Following the example in the comments, I added the following

# 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, .

+4
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() , , . , .

, .

+1

Source: https://habr.com/ru/post/1584633/


All Articles