简体   繁体   中英

How to “live” update matplotlib figure plot using trace

There are few anchor points to notice in the code.

The update_observation() function is updated every time a drop downs value changes. I want the graph to change as well whenever a dropdown list value is changed. I would like only one instance of the graph window at any point. I want to update the information inside the graph window. I tried using animation but I believe there could be an alternative to that using the trace method.

The button is simply to open and close the window. And it solves no other purpose. It binds until the same window is not closed. How do I use the value of artifact_graph_window in my update_observation() function.

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()

The catch is to keep the scope of the animation function correct and along with that the canvas, figure and axes names need to be different for different windows. You can set the interval and the blit param to make the update more efficient.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM