fy_闷油瓶 2022-04-15 22:34 采纳率: 0%
浏览 48

使用matplotlib在tkinter中绘制波形,对波形放大后为什么自动缩小

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.")

这是波形放大后

img

波形放大后,会立马又缩小:

img


不能一直保持放大状态

怎么让连续采集时,波形可以一直保持放大状态(对新一组的数据会自动保持之前的放大比例进行放大)
  • 写回答

1条回答 默认 最新

  • A Python 萌新花花 2022-04-16 11:20
    关注

    set_xlim 和 set_ylim直接锁定你要的数据位置?但是这样明显是取巧而且很麻烦的方法

    评论

报告相同问题?

问题事件

  • 创建了问题 4月15日

悬赏问题

  • ¥15 C#.net通过内网url地址获取文件并下载问题,浏览器postman可以正常下载,用程序不行
  • ¥15 本人本科机械,目前研一。没有深度学习基础,目前对研究生课题一片迷茫,请教各位!
  • ¥15 关于R语言单因素与多因素线性回归的平均值
  • ¥15 服务器清除BIOS之后引导不了
  • ¥15 CPLEX用OPL编写的混合整数线性优化问题。
  • ¥15 可以用EasyConnect连接实验室内网,但无法连接内网才能访问的服务器,为什么?
  • ¥15 前端预览docx文件,文件从后端传送过来。
  • ¥15 层次聚类和蛋白质相似度
  • ¥25 主成分分析中的第一第二主成分分别代表哪些参数
  • ¥15 oracle数据库查询语句问题