Python Matplotlib按钮
发布时间:2020-12-20 13:49:03 所属栏目:Python 来源:网络整理
导读:我是一个可怜的程序员,所以请原谅我的简单问题. 我正在尝试构建一个从串行接口读取数据并将其显示在屏幕上的小程序.我已经能够在i Python notebook和matplotlib中做到这一点,我已经能够在屏幕上添加按钮来控制进入界面的数据请求: 按钮点击 – ser.write,se
我是一个可怜的程序员,所以请原谅我的简单问题.
我正在尝试构建一个从串行接口读取数据并将其显示在屏幕上的小程序.我已经能够在i Python 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,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,2048) line1,line2 = plt.plot(t,0*t,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.075]) axstart = plt.axes([0.40,0.075]) axstop = plt.axes([0.51,0.075]) bone = Button(axone,'1') bone.on_clicked(one) btwo = Button(axtwo,'2') btwo.on_clicked(two) 按照评论中引用的例子,我添加了以下内容 # Build check button axes rax = plt.axes([0.7,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中断内核时才会构建屏幕. 解决方法
如果要调用常规读取数据的函数,可以在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,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,'2') btwo.on_clicked(two) plt.show() 另一件事是如果我没有在你的代码中添加plt.show()行,它在我的计算机中运行时没有显示该数字.所以我在代码的末尾添加它,它现在可以显示数字. 希望能帮助到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |