Python-matplotlib实时图形无图形移动(滚动)

Vas*_*K V 6 python graph matplotlib

我目前正在开发一个Python应用程序,我已经开始从头开始编写了.已有的是:

  1. 在后台运行的线程从服务器收集数据并写入文件.
  2. 在主线程中,我打开了一个matplotlib图形GUI,它绘制了我在第1点中提到的这个文件的实时数据.

所以事情是非常好的,按照我上面解释的那样工作.但是当matplotlib正在绘制实时数据时,当新点被添加到它所引用的后端文件时,图形会动态移动.我不确定我是否在这里表达了我的问题.我的意思是说,当实时图形更新时,图形屏幕实际上是向前移动(从左到右),旧图形(旧点)变得不可见(当屏幕移动时)并丢失.如果我尝试向左滚动(使用默认matplotlib工具提供的滚动工具),那么只有空白区域.我想要的是:

  1. 屏幕不应该移动.
  2. 旧点应该是可见的而不向左滚动.
  3. 只应添加新点,旧点不应被清除或移动(隐藏).

这是我的代码:

#import modules
import Tkinter as tk


import MasterWindow
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from threading import Thread
from datetime import *
import matplotlib.dates as dates


from fabfile import connect
import time
from fabric.api import env

LARGE_FONT= ("Verdana", 12)

env.host_string = 'nms@10.0.0.70'
env.password = "nms"

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)      
        label = tk.Label(self, text="Graphy-Home", font=LARGE_FONT)     
        label.pack(pady=10,padx=10)     
        Command = tk.Label(self, text="Enter Command")      
        pickCommand = tk.Entry(self)        
        pickCommand.pack(pady=10)           
        Command.pack()  
        button1 = tk.Button(self, text="Submit Command", command=lambda: submit())      
        button1.pack()

        fig = plt.figure()
        ax1 = fig.add_subplot(1,1,1)
        def submit():       
            t1=Thread(target=FileWriter)
            t1.start()
            ani = animation.FuncAnimation(fig, animate, interval=1000)
            plt.show()
            print("done")

        def FileWriter():
            f=open('F:\\home\\WorkSpace\\FIrstPyProject\\TestModules\\sampleText.txt','w',0)
            k=0
            cmd=pickCommand.get()
            while (k < 40):
                print("inside while")
                p=connect(cmd)
                f.write(p.stdout)
                f.write('\n')
                print("wrote data")
                k += 1

        def animate(i):
            print("inside animate")
            pullData = open("sampleText.txt","r").read()
            dataArray = pullData.split('\n')
            xar = []
            yar = []
            for eachLine in dataArray:
                if len(eachLine)>1:
                    x,y = eachLine.split(',')
                    timeX=datetime.strptime(x, "%H:%M:%S")
                    xar.append(timeX.strftime("%H:%M:%S"))
                    yar.append(float(y))


            #ax1.plot(timeStamps,yar)
            plt.plot_date(dates.datestr2num(xar), yar,'b-')

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2.pack()

app = MasterWindow.MasterWindow(StartPage, PageOne, PageTwo)
app.mainloop()
Run Code Online (Sandbox Code Playgroud)

只是为了抬头,animate函数是在主循环中运行的函数,FileWriter函数在一个单独的线程中运行以收集数据.

我希望以一种可以理解的方式解释我的问题!如果有任何不清楚的地方,请告诉我.

小智 0

不确定,因为我无法运行你的代码。但我建议尝试添加

    ax1.axis([xmin, xmax, ymin, ymax])
Run Code Online (Sandbox Code Playgroud)

    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
Run Code Online (Sandbox Code Playgroud)