matplotlib在tkinter中绘制波形,在连续采集数据的模式下,对波形进行放大,波形放大一瞬间,立马就会变回原样,不能让波形一直保持放大的状态
代码如下
if __name__ == '__main__':
root = tk.Tk()
root.title("The Otter pyCCDGUI")
# root.resizable(0, 0)
SerQueue = queue.Queue()
menu = CCDmenusetup.buildmenu(root)
CCDplot = CCDplots.BuildPlot(root)
panel = CCDpanelsetup.buildpanel(root, CCDplot, SerQueue)
panel.grid(row=0, column=2)
root.mainloop()
class BuildPlot(tk.Frame):
def __init__(self, master):
# create canvas
self.f = Figure(figsize=(10, 5), dpi=100, tight_layout=True)
self.a = self.f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
self.a.plot(t, s, linewidth=0.6)
# a tk.DrawingArea
self.canvas = FigureCanvasTkAgg(self.f, master=master)
self.canvas.draw()
self.canvas.get_tk_widget().grid(row=0, columnspan=2)
toolbarFrame = tk.Frame(master=master)
toolbarFrame.grid(row=1, columnspan=2, sticky="w")
NavigationToolbar2Tk(self.canvas, toolbarFrame)
# 连续数据采集
def rxtxcontthread(panel, progress_var):
# open serial port
try:
ser = serial.Serial(config.port, config.baudrate)
# disable controls
panelsleep(panel)
config.stopsignal = 0
# restart the progressbar for each acquisition
panel.progress.config(mode="indeterminate")
panel.progress.start(100)
# threadprogress = threading.Thread(target=progressthread, args=(progress_var), daemon=True)
# threadprogress.start()
# wait to clear the input and output buffers, if they're not empty data is corrupted
while (ser.in_waiting > 0):
ser.reset_input_buffer()
ser.reset_output_buffer()
time.sleep(0.1)
# Transmit key 'ER'
config.txfull[0] = 69
config.txfull[1] = 82
# split 32-bit integers to be sent into 8-bit data
config.txfull[2] = (config.SHperiod >> 24) & 0xff
config.txfull[3] = (config.SHperiod >> 16) & 0xff
config.txfull[4] = (config.SHperiod >> 8) & 0xff
config.txfull[5] = config.SHperiod & 0xff
config.txfull[6] = (config.ICGperiod >> 24) & 0xff
config.txfull[7] = (config.ICGperiod >> 16) & 0xff
config.txfull[8] = (config.ICGperiod >> 8) & 0xff
config.txfull[9] = config.ICGperiod & 0xff
# averages to perfom
# config.txfull[10] = config.AVGn[0]
# config.txfull[11] = config.AVGn[1]
config.txfull[10] = config.AVGn[0]
config.txfull[11] = config.AVGn[1]
# transmit everything at once (the USB-firmware does not work if all bytes are not transmittet in one go)
ser.write(config.txfull)
# loop to acquire and plot data continuously
while (config.stopsignal == 0):
# wait for the firmware to return data
config.rxData8 = ser.read(7388)
if (config.stopsignal == 0):
# combine received bytes into 16-bit data
for rxi in range(3694):
config.rxData16[rxi] = (config.rxData8[2 * rxi + 1] << 8) + config.rxData8[2 * rxi]
# plot the new data
# 绘制新的波形
panel.bupdate.invoke()
# hold values for saving data to file
config.SHsent = config.SHperiod
config.ICGsent = config.ICGperiod
# resend settings with continuous transmission disabled to avoid flooding of the serial port
config.txfull[10] = 0
ser.write(config.txfull)
# wait until data is received to close the serial port
while (ser.out_waiting > 0):
time.sleep(0.1)
# close serial port
ser.close()
panelwakeup(panel)
panel.progress.stop()
except serial.SerialException:
messagebox.showerror("By the great otter!", "There's a problem with the specified serial connection.")
这是波形放大后
波形放大后,会立马又缩小:
不能一直保持放大状态