Python Matplotlib Buttons -


i poor programmer please excuse simple question. trying build little program reads data serial interface , displays on screen. have been able in ipython notebook , matplotlib , have been able add buttons screen control data requests go interface: button click -> ser.write, ser.read, draw

i struggling design program such pressing button start repeated data collection in fixed time steps until button toggled off again. can please me out sketch such program?

so far:

%pylab   import matplotlib.pyplot plt import numpy np matplotlib.widgets import button  import serial import binascii import struct 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 responding") else:     print ("interface 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 example cited in comments, added 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) 

but loop @ bottom not how it. when start program it, graphics window opens, doesn't show anything. if interrupt kernel in ipython screen builds.

if want call function reads data routinely, can use timer in matplotlib module. code following:

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

the unit of interval ms, , can use timer.start() or timer.stop() methods turn on or turn off timer.


based on code, add timers each button , add variables check whether timers running:

import matplotlib.pyplot plt import numpy np matplotlib.widgets import button  import serial import binascii import struct 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 responding") else:     print ("interface 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() 

another 1 thing if did not add line plt.show() in code, did not show figure when ran in computer. add in end of code , can show figure now.

hope helps.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -