简体   繁体   中英

How to update graph in canvas?

Im making a GUI which is visualizing json data from a. The problem im currently stuck at is that i cant get the graph to update in the canvas, it only updates when i press the max/min button: 在此处输入图像描述 . Is there a way to update the graph when i press a new file? Picture of the GUI: 在此处输入图像描述

Part of the code plotting the graph:

##### Canvas ####
figure = plt.figure()
canvas = FigureCanvasTkAgg(figure, master=inspect_data)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)


###### file gets selected #####


def items_selected(d):
    plt.clf()
    selected_indices = vores_listebox.curselection()
    selected_json = ",".join([vores_listebox.get(i) for i in selected_indices])
    full_file_path = path_us + selected_json
    open_json = js.load(open(full_file_path, "r"))
    time = [open_json['XML_Data']
            ['Wsk3Vectors']['X_Axis']['Values']['float']]
    rpm = [open_json['XML_Data']['Wsk3Vectors']
           ['Y_AxesList']['AxisData'][0]['Values']['float']]
    torque = [open_json['XML_Data']['Wsk3Vectors']
              ['Y_AxesList']['AxisData'][1]['Values']['float']]
    current = [open_json['XML_Data']['Wsk3Vectors']
               ['Y_AxesList']['AxisData'][2]['Values']['float']]
    angle = [open_json['XML_Data']['Wsk3Vectors']
             ['Y_AxesList']['AxisData'][3]['Values']['float']]
    depth = [open_json['XML_Data']['Wsk3Vectors']
             ['Y_AxesList']['AxisData'][4]['Values']['float']]


###### Using Matlib.pyplot to plot 5 graphs ######
    plt.rcParams["figure.figsize"] = (7, 10)

    plt.subplot(5, 1, 1)
    plt.scatter(time, rpm, c="b", linewidths=2,
                marker=",", edgecolor="b", s=1, alpha=0.5)
    plt.title(selected_json)
    plt.gca().axes.xaxis.set_ticklabels([])
    plt.ylabel("RPM")
    plt.grid()

    plt.subplot(5, 1, 2)
    plt.scatter(time, torque, c="g", linewidths=1,
                marker=",", edgecolor="g", s=1, alpha=0.3)
    plt.gca().axes.xaxis.set_ticklabels([])
    plt.ylabel("Torque [Nm]")
    plt.grid()

    plt.subplot(5, 1, 3)
    plt.scatter(time, current, c="r", linewidths=2,
                marker=",", edgecolor="r", s=1, alpha=0.5)
    plt.gca().axes.xaxis.set_ticklabels([])
    plt.ylabel("Current [Amps]")
    plt.grid()

    plt.subplot(5, 1, 4)
    plt.scatter(time, angle, c="m", linewidths=2,
                marker=",", edgecolor="m", s=1, alpha=0.5)
    plt.gca().axes.xaxis.set_ticklabels([])
    plt.ylabel("Angle [RAD]")
    plt.grid()

    plt.subplot(5, 1, 5)
    plt.scatter(time, depth, c="c", linewidths=2,
                marker=",", edgecolor="c", s=1, alpha=0.5)
    plt.xlabel("Time [ms]")
    plt.ylabel("Depth [mm]")
    plt.grid()


vores_listebox.bind('<<ListboxSelect>>', items_selected)

I have tried root.update(), Canvas.update(), and a lot of other commands, but nothing works. Any help is appreciated!

Have you tried calling canvas.draw_idle() or perhaps canvas.draw() where/when you want to refresh the canvas widget?

def items_selected(d):
    plt.clf()
    selected_indices = vores_listebox.curselection()
    selected_json = ",".join([vores_listebox.get(i) for i in selected_indices])
    full_file_path = path_us + selected_json
    open_json = js.load(open(full_file_path, "r"))
    time = [open_json['XML_Data']
            ['Wsk3Vectors']['X_Axis']['Values']['float']]
    rpm = [open_json['XML_Data']['Wsk3Vectors']
           ['Y_AxesList']['AxisData'][0]['Values']['float']]
    torque = [open_json['XML_Data']['Wsk3Vectors']
              ['Y_AxesList']['AxisData'][1]['Values']['float']]
    current = [open_json['XML_Data']['Wsk3Vectors']
               ['Y_AxesList']['AxisData'][2]['Values']['float']]
    angle = [open_json['XML_Data']['Wsk3Vectors']
             ['Y_AxesList']['AxisData'][3]['Values']['float']]
    depth = [open_json['XML_Data']['Wsk3Vectors']
             ['Y_AxesList']['AxisData'][4]['Values']['float']]
    
    canvas.draw_idle()  # refresh the canvas with new data

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