如何使用跟踪“实时”更新matplotlib图

时间:2018-09-20 14:10:30

标签: python python-3.x matplotlib tkinter

代码中很少有锚点要注意。

每次下拉值更改时,update_observation()函数都会更新。每当下拉列表值更改时,我也希望图形也更改。我在任何时候都只想要一个图形窗口实例。我想更新图形窗口内的信息。我尝试使用动画,但我相信使用trace方法可以替代动画。

该按钮仅用于打开和关闭窗口。它没有解决任何其他目的。它将绑定,直到未关闭同一窗口。如何在artifact_graph_window函数中使用update_observation()的值。

def tab_two_load(self,tab1_open_observations):
    def update_observation(a,b,c):
        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)

        ##CONFIG 
        fig = Figure(figsize=(6,6))
        a = fig.add_subplot(111)

        ## DATA
        x_cat = [x[1] for x in db.query_artifact()]
        x_cat = x_cat[:-2]

        y_count = {}
        for i in x_cat:
            y_count[i] = 0

        for i in observations_fr_project:
            y_count[i[2]] = y_count[i[2]] + 1

        print(y_count)

        x = y_count.keys()
        y = y_count.values()

        ## PLOT
        a.bar(x,y,color='blue')

        ## SHOW
        a.set_title ("Artifact Breakdown", fontsize=16)
        a.set_ylabel("Breakdown", fontsize=14)
        a.set_xlabel("Artifact Type", fontsize=14)
        a.yaxis.set_major_locator(MaxNLocator(integer=True))

        #######################################
        ''' GRAPH END '''
        #######################################

        canvas = FigureCanvasTkAgg(fig, master=artifact_graph_window)
        canvas.get_tk_widget().pack()
        canvas.draw()   


    def artifact_graph():
        artifact_graph_window = Toplevel(self)
        artifact_graph_window.title('Artifact Graph')
        button_graph_1.config(state='disable')

        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)

        def quit_win():
            artifact_graph_window.destroy()
            button_graph_1.config(state='normal')

        quit_artifact_graph = Button(artifact_graph_window, text='Quit', command=quit_win)
        quit_artifact_graph.pack()

        artifact_graph_window.protocol("WM_DELETE_WINDOW", quit_win) 

    open_obs_result = db.query_open_findings()
    open_obs_count = len(open_obs_result)

    lbl_open_obs = Label(tab1_open_observations, text='Open Observations')
    lbl_open_obs.grid(row=0, column = 0,padx=10, pady=10)

    lbl_open_obs_count = Label(tab1_open_observations, text='Open Project/Modules ')
    lbl_open_obs_count.grid(row=1, column=0,padx=10, pady=10)
    lbl_open_obs_count_number = Label(tab1_open_observations, text=open_obs_count)
    lbl_open_obs_count_number.grid(row=1, column=1,padx=10, pady=10)

    observation_project_value = StringVar(tab1_open_observations,value='Default Text')
    observation_project_value.trace('w',update_observation)

    lbl_observation_project = Label(tab1_open_observations, text='Open Projects ')
    lbl_observation_project.grid(row=2, column=0,padx=10, pady=10)

    observation_project = [str(obs[0]) + ' -- ' + str(obs[1]) for obs in open_obs_result]
    observation_project_value.set(observation_project[0])
    observation_project_field = OptionMenu(tab1_open_observations,observation_project_value,*observation_project)
    observation_project_field.grid(row=2, column=1,padx=10, pady=10) 

    button_graph_1 = Button(tab1_open_observations, text="Artifact Graph", command=artifact_graph)
    button_graph_1.grid(row=4, column=0,padx=20, pady=20)   
    button_graph_1.bind()

1 个答案:

答案 0 :(得分:0)

要注意的是保持动画功能的范围正确,并且对于不同的窗口,画布,图形和轴的名称也需要不同。您可以设置intervalblit参数以使更新更有效。

def tab_two_load(self,tab1_open_observations):
    ######################################################################################
    #TAB1
    #TAB1 - TAB2
    ######################################################################################
    ##CONFIG 
    fig_artif = Figure(figsize=(6,6))
    fig_findin = Figure(figsize=(6,6))
    fig_severity = Figure(figsize=(6,6))

    ax = fig_artif.add_subplot(111)
    ay = fig_findin.add_subplot(111)
    az = fig_severity.add_subplot(111)

    def animate_artifact_graph(i):
        #######################################
        ''' GRAPH START '''
        #######################################
        ## DATA
        x_cat = [x[1] for x in db.query_artifact()]
        x_cat = x_cat[:-2]

        y_count = {}
        for i in x_cat:
            y_count[i] = 0

        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)
        title = proj_name + " - " + mod_name

        for i in observations_fr_project:
            y_count[i[2]] = y_count[i[2]] + 1

        print(y_count)

        x = y_count.keys()
        y = y_count.values()

        ## SHOW
        ax.clear()
        ax.bar(x,y,color='blue')
        ax.set_title (title, fontsize=16)
        ax.set_ylabel("Breakdown", fontsize=14)
        ax.set_xlabel("Artifact Type", fontsize=14)
        ax.yaxis.set_major_locator(MaxNLocator(integer=True))

        #######################################
        ''' GRAPH END '''
        #######################################

    def animate_finding_graph(i):
        #######################################
        ''' GRAPH START '''
        #######################################
        ## DATA
        x_cat = [x[1] for x in db.query_artifact()]
        x_cat = x_cat[:-2]

        y_count = {}
        for i in x_cat:
            y_count[i] = 0

        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)
        title = proj_name + " - " + mod_name

        for i in observations_fr_project:
            y_count[i[2]] = y_count[i[2]] + 1

        print(y_count)

        x = y_count.keys()
        y = y_count.values()

        ## SHOW
        ay.clear()
        ay.bar(x,y,color='yellow')
        ay.set_title (title, fontsize=16)
        ay.set_ylabel("Top 5 Finding Category", fontsize=14)
        ay.set_xlabel("Value", fontsize=14)
        ay.yaxis.set_major_locator(MaxNLocator(integer=True))

        #######################################
        ''' GRAPH END '''
        #######################################

    def animate_severity_graph(i):
        #######################################
        ''' GRAPH START '''
        #######################################
        ## DATA
        x_cat = [x[1] for x in db.query_artifact()]
        x_cat = x_cat[:-2]

        y_count = {}
        for i in x_cat:
            y_count[i] = 0

        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)
        title = proj_name + " - " + mod_name

        for i in observations_fr_project:
            y_count[i[2]] = y_count[i[2]] + 1

        print(y_count)

        x = y_count.keys()
        y = y_count.values()

        ## SHOW
        az.clear()
        az.bar(x,y,color='red')
        az.set_title (title, fontsize=16)
        az.set_ylabel("Top 5 Finding Category", fontsize=14)
        az.set_xlabel("Value", fontsize=14)
        az.yaxis.set_major_locator(MaxNLocator(integer=True))

        #######################################
        ''' GRAPH END '''
        #######################################

    def update_observation(a,b,c):
        proj_name,mod_name = observation_project_value.get().split(" -- ")
        observations_fr_project = db.query_findings_by_project(proj_name,mod_name)
        title_text = proj_name + " - " + mod_name

    def artifact_graph():
        artifact_graph_window = Toplevel(self)
        artifact_graph_window.title('Artifact Graph')
        button_graph_1.config(state='disable')

        canvas_artifact = FigureCanvasTkAgg(fig_artif, master=artifact_graph_window)
        canvas_artifact.get_tk_widget().pack()
        ani = animation.FuncAnimation(fig_artif, animate_artifact_graph,interval=500)
        canvas_artifact.draw()   

        def quit_win():
            artifact_graph_window.destroy()
            button_graph_1.config(state='normal')

        quit_artifact_graph = Button(artifact_graph_window, text='Quit', command=quit_win)
        quit_artifact_graph.pack()

        artifact_graph_window.protocol("WM_DELETE_WINDOW", quit_win) 

    def finding_category():
        finding_cat_graph_window = Toplevel(self)
        finding_cat_graph_window.title('Finding Graph')
        button_graph_2.config(state='disable')

        canvas_finding = FigureCanvasTkAgg(fig_findin, master=finding_cat_graph_window)
        canvas_finding.get_tk_widget().pack()
        ani = animation.FuncAnimation(fig_findin, animate_finding_graph,interval=500)
        canvas_finding.draw() 

        def quit_win():
            finding_cat_graph_window.destroy()
            button_graph_2.config(state='normal')

        quit_finding_graph = Button(finding_cat_graph_window, text='Quit', command=quit_win)
        quit_finding_graph.pack()

        finding_cat_graph_window.protocol("WM_DELETE_WINDOW", quit_win) 

    def severity():
        severity_graph_window = Toplevel(self)
        severity_graph_window.title('Severity Graph')
        button_graph_3.config(state='disable')

        canvas_severity = FigureCanvasTkAgg(fig_severity, master=severity_graph_window)
        canvas_severity.get_tk_widget().pack()
        ani = animation.FuncAnimation(fig_severity, animate_severity_graph,interval=500)
        canvas_severity.draw() 

        def quit_win():
            severity_graph_window.destroy()
            button_graph_3.config(state='normal')

        quit_severity_graph = Button(severity_graph_window, text='Quit', command=quit_win)
        quit_severity_graph.pack()

        severity_graph_window.protocol("WM_DELETE_WINDOW", quit_win) 


    open_obs_result = db.query_open_findings()
    open_obs_count = len(open_obs_result)

    lbl_open_obs = Label(tab1_open_observations, text='Open Observations')
    lbl_open_obs.grid(row=0, column = 0,padx=10, pady=10)

    lbl_open_obs_count = Label(tab1_open_observations, text='Open Project/Modules ')
    lbl_open_obs_count.grid(row=1, column=0,padx=10, pady=10)
    lbl_open_obs_count_number = Label(tab1_open_observations, text=open_obs_count)
    lbl_open_obs_count_number.grid(row=1, column=1,padx=10, pady=10)

    observation_project_value = StringVar(tab1_open_observations,value='Default Text')
    observation_project_value.trace('w',update_observation)

    lbl_observation_project = Label(tab1_open_observations, text='Open Projects ')
    lbl_observation_project.grid(row=2, column=0,padx=10, pady=10)
    observation_project = [str(obs[0]) + ' -- ' + str(obs[1]) for obs in open_obs_result]
    observation_project_value.set(observation_project[0])
    observation_project_field = OptionMenu(tab1_open_observations,observation_project_value,*observation_project)
    observation_project_field.grid(row=2, column=1,padx=10, pady=10) 

    button_graph_1 = Button(tab1_open_observations, text="Artifact Graph", command=artifact_graph)
    button_graph_1.grid(row=4, column=0,padx=20, pady=20)   
    button_graph_1.bind()

    button_graph_2 = Button(tab1_open_observations, text="Finding Category", command=finding_category)
    button_graph_2.grid(row=4, column=1,padx=20, pady=20)   
    button_graph_2.bind()

    button_graph_3 = Button(tab1_open_observations, text="Severity", command=severity)
    button_graph_3.grid(row=4, column=2,padx=20, pady=20)   
    button_graph_3.bind()