Running Tkinter canvas draw function again starts from latest datapoint instead of creating new line

Viewed 15

I have a canvas where i draw lines repeadetly after adding new values to the array. The plot shows sensor data over time. Now when i want to start the drawing again from the beginning, so that the startpoint on the time axis is again 0 (as it is in the array), the canvas draws a line from the first to the last datapoint of the first run to zero from the new run, but i want to start without connecting to the first and last point from the old drawing. I only want the new line to be drawn. How can i achieve this?

Below is a cutout of the code i use and two pictures showing the problem. Feel free to ask if there is information missing.

def plot_graph():
    while condition == True:
        timePlot = np.append(timePlot, ExChange[0])
        dataPlot1 = np.append(dataPlot1, ExChange[1])
        dataPlot2 = np.append(dataPlot2, ExChange[3])
        lines1.set_xdata(timePlot)
        lines1.set_ydata(dataPlot1)
        lines2.set_xdata(timePlot)
        lines2.set_ydata(dataPlot2)
        
        canvas.draw()

if __name__ == '__main__':

    # ------------------ Create frame for Control ------------------#
    frm_frameC = tk.LabelFrame(root, padx=10, pady=10, bg='light grey', bd=3, width=1650, height=800)
    frm_frameC.place(x=0, y=0)

    # ------------------ Create figure for plotting ------------------#
    fig = Figure()
    ax = fig.add_subplot(111)
    ax.set_title('Data monitoring')
    ax.set_xlabel('time in [s]')
    ax.set_ylabel('[l/min]')
    ax.set_xlim(0, 30)
    ax.set_ylim(0, 101)
    ax2 = ax.twinx()
    ax2.set_ylabel('[l]')
    ax2.set_ylim(0, 1001)
    lines1 = ax.plot([], [], color='saddlebrown')[0]
    lines2 = ax.plot([], [], color='peru')[0]
    lines3 = ax2.plot([], [], color='sandybrown')[0]
    lines4 = ax.plot([], [], color='navy')[0]
    lines5 = ax.plot([], [], color='royalblue')[0]
    lines6 = ax2.plot([], [], color='dodgerblue')[0]
    lines7 = ax.plot([], [], color='darkgreen')[0]
    lines8 = ax.plot([], [], color='limegreen')[0]
    lines9 = ax2.plot([], [], color='lightgreen')[0]
    canvas = FigureCanvasTkAgg(fig, master=frm_frameC)
    canvas.get_tk_widget().place(x=5, y=5, width=1000, height=400)
    canvas.draw()

The ExChange array is cleared completely when running the plot_graph and gets filled by an process running with multiprocessing.

Plot after first run

Plot after second run. The new line starting at about 0.4 secs is connected to the last point of the previous run at the right edge (about 7.7 secs)

0 Answers
Related