如何将动态matplotlib图转移到Tkinter GUI上?

时间:2019-02-04 00:09:53

标签: python-3.x matplotlib tkinter tkinter-canvas

我有一个数据循环,可收集数据并将其图形化到笔记本matplotlib图形上,该图形会不断更新。如何将此图传输到Tkinter GUI上?

我尝试使用不同的Canvas选项,但是由于缺乏编码经验,我通常会遇到一些错误,我不知道如何解决。我还尝试更改以下代码,以便将x和y值写入通用data.txt文件中,并从中读取GUI图,但是我一直在坚持如何获取tkinter canvas图来读取它们而我的原始数据收集代码仍在编写中。以下是用于在笔记本上绘制图形的代码。 def controltemperature(self,Temperature):

        ####################################
        T=Temperature# Change this target Temperature
        #####################################
        I_upper = 2.5
        I_lower = 0.1
        A = (T-3)
        B = T+3
        N = 1
        I_range = 1.0
        T_dynamical_range = 0.6
        current_T = 0
        previous_T = 0
        f = plt.figure()
        a = f.add_subplot(111)
        f.show()
        i = 0
        x, y = [], []
        with nidaqmx.Task() as task:
            task.ai_channels.add_ai_thrmcpl_chan("Dev3/ai0")
            data = task.read(number_of_samples_per_channel=N)
        while data[0]<(A):
            with nidaqmx.Task() as task:
                task.ai_channels.add_ai_thrmcpl_chan("Dev3/ai0")
                data = task.read(number_of_samples_per_channel=N)
            self.setcurrentTenma722705("%.*f" % (2, I_upper))
            x.append(i)
            y.append(data[0])
            a.plot(x, y, color='b')
            f.canvas.draw()############################################
            a.set_xlim(left=max(0, i-50), right=i+50)
            a.set_ylim([0,200])
            i += 1
        while data[0]>B:
            with nidaqmx.Task() as task:
                task.ai_channels.add_ai_thrmcpl_chan("Dev3/ai0")
                data = task.read(number_of_samples_per_channel=N)
            self.setcurrentTenma722705("%.*f" % (2, I_lower))
            x.append(i)
            y.append(data[0])
            a.plot(x, y, color='b')
            f.canvas.draw()############################################
            a.set_xlim(left=max(0, i-50), right=i+50)
            a.set_ylim([0,200])
            i += 1
        current_T = data[0]
        previous_T = data[0]
        while True:
        # Heat up loop
            while current_T<T:
                with nidaqmx.Task() as task:
                    task.ai_channels.add_ai_thrmcpl_chan("Dev3/ai0")
                    data = task.read(number_of_samples_per_channel=N)
                current_T = data[0]
                # This section is for decreasing the I_upper##############################
                if (current_T - previous_T)>0.1: #0.1 and 0.05 tunable    # if the derivative of T is too large
                    I_upper -= 0.05   
                previous_T = current_T
                if (current_T -T)< -T_dynamical_range and (I_lower+I_range)<I_upper: # if value of T is too large
                    I_lower = (I_upper-I_range) - ((I_upper-I_range)-I_lower)*(1.0-0.1*T_dynamical_range) # 0.1 tunable
                ##############################################################################
                self.setcurrentTenma722705("%.*f" % (2, I_upper))
                x.append(i)
                y.append(data[0])
                a.plot(x, y, color='b')
                f.canvas.draw()############################################
                a.set_xlim(left=max(0, i-50), right=i+50)
                a.set_ylim([0,200])
                i += 1
                #Cool down loop
            while current_T >T:
                with nidaqmx.Task() as task:
                    task.ai_channels.add_ai_thrmcpl_chan("Dev3/ai0")
                    data = task.read(number_of_samples_per_channel=N)
                current_T = data[0]
                ###############################################################################
                if (current_T - previous_T)< -0.1: #0.1 and 0.05 tunable
                    I_lower = I_lower + 0.05   
                previous_T = current_T
                if (current_T -T)> T_dynamical_range and (I_lower+I_range)<I_upper:
                    I_upper = (I_lower+I_range) + (I_upper-(I_lower+I_range))*(1.0-0.1*T_dynamical_range)   # 0.1 tunable 
                ###########################################################################
                self.setcurrentTenma722705("%.*f" % (2, I_lower))
                x.append(i)
                y.append(data[0])
                a.plot(x, y, color='b')
                f.canvas.draw()############################################
                a.set_xlim(left=max(0, i-50), right=i+50)
                a.set_ylim([0,200])
                i += 1
        T_dynamical_range =0.05+ 0.95 *(T_dynamical_range-0.05) #0.05 and 0.95 tunable
        I_range = 0.2 + 0.95*(I_range - 0.2)#0.2 and 0.95 tunable

    except(KeyboardInterrupt, SystemExit):
        print("Program stopped manually")            
#             self.setvoltageTenma722705("0.0")
        self.setcurrentTenma722705("0.0")

我只想让我的笔记本图形出现在我的tkinter gui上

0 个答案:

没有答案