Всем привет! Начал изучать питон с целью написания простенького графического окошка для отображения получаемого по сети состояния моей системы. Столкнулся с проблемой, рисую графики при помощи matplotlib, анимирую их и пытаюсь встроить в общее окно программы. Встраиваться то оно встраивается, но исходное окно при этом не закрывается, хотя анимация на нем не идет, а идет только на встроенном окне. Поиск по форумам решения не дал. Опыта на питоне у меня неделя, понять в чем причина компетенции не хватает, помогите пожалуйста разобраться!
Скрины не прикрепились, доступны по ссылкам:
https://disk.yandex.ru/i/Dvsi7UX6fCchEw
https://disk.yandex.ru/i/qt0cT6tSZsLWpA
 #!/usr/bin/env python
import struct
import time
import PySimpleGUI as sg
import matplotlib.pyplot as plt  # $ pip install matplotlib
import matplotlib.animation as animation
from collections import deque
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from socket import *
def draw_figure(canvas, figure):
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
    figure_canvas_agg.draw()
    return figure_canvas_agg
def delete_fig_agg(fig_agg):
    fig_agg.get_tk_widget().forget()
    plt.close('all')
def new_subplot(y,x,n,fig):
    ax = fig.add_subplot(y, x, n)
    ax.set_autoscale_on(True)
    plt.ylabel('HC#'+ str(n))
    plt.xlabel('time, sec')
    plt.xticks()
    plt.yticks()
    plt.grid()
    ax.grid(linestyle="-", which='major', linewidth=0.5, color='.25', zorder=-10)
    ax.grid(linestyle="-", which='minor', linewidth=0.25, color='.75', zorder=-10)
    return ax
def update_animation(i, cur_time, x, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12):
    #data1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    data_flag = False
    while 1:
        try:
            data, addr = udp_socket.recvfrom(1024)
            data1 = struct.unpack("fffffffffffff", data)
            data_flag = True
        except Exception:
            break
    timing_ = time.time()-cur_time
    x.append(timing_)
    if data_flag:
        y1.append(data1[3])
        y2.append(data1[4])
        y3.append(data1[5])
        y4.append(data1[6])
        y5.append(data1[7])
        y6.append(data1[8])
        y7.append(data1[9])
        y8.append(data1[10])
        y9.append(data1[11])
        y10.append(data1[12])
        y11.append(data1[3])
        y12.append(data1[4])
    else:
        y1.append(y1[npoints - 1])
        y2.append(y2[npoints - 1])
        y3.append(y3[npoints - 1])
        y4.append(y4[npoints - 1])
        y5.append(y5[npoints - 1])
        y6.append(y6[npoints - 1])
        y7.append(y7[npoints - 1])
        y8.append(y8[npoints - 1])
        y9.append(y9[npoints - 1])
        y10.append(y10[npoints - 1])
        y11.append(y11[npoints - 1])
        y12.append(y12[npoints - 1])
    line1.set_data(x, y1)
    line2.set_data(x, y2)
    line3.set_data(x, y3)
    line4.set_data(x, y4)
    line5.set_data(x, y5)
    line6.set_data(x, y6)
    line7.set_data(x, y7)
    line8.set_data(x, y8)
    line9.set_data(x, y9)
    line10.set_data(x, y10)
    line11.set_data(x, y11)
    line12.set_data(x, y12)
    ax1.set_ylim(-1.1, 1.1)
    ax1.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    ax2.set_ylim(-1.1, 1.1)
    ax2.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    ax3.set_ylim(-1.1, 1.1)
    ax3.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    ax4.set_ylim(-1.1, 1.1)
    ax4.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    ax5.set_ylim(-1.1, 1.1)
    ax5.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    ax6.set_ylim(-1.1, 1.1)
    ax6.set_xlim(x[npoints-1]-40.5, x[npoints-1]+0.5)
    return line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line12, ax1, ax2, ax3, ax4, ax5, ax6
host = '192.168.0.44'
port = 5051
addr = (host, port)
udp_socket = socket(AF_INET, SOCK_DGRAM)
udp_socket.setblocking(False)
udp_socket.bind(addr)
npoints = 1000
x = deque([0.0], maxlen=npoints)
y1 = deque([0.0], maxlen=npoints)
y2 = deque([0.0], maxlen=npoints)
y3 = deque([0.0], maxlen=npoints)
y4 = deque([0.0], maxlen=npoints)
y5 = deque([0.0], maxlen=npoints)
y6 = deque([0.0], maxlen=npoints)
y7 = deque([0.0], maxlen=npoints)
y8 = deque([0.0], maxlen=npoints)
y9 = deque([0.0], maxlen=npoints)
y10 = deque([0.0], maxlen=npoints)
y11 = deque([0.0], maxlen=npoints)
y12 = deque([0.0], maxlen=npoints)
for num in range(npoints):
    x.append(0.0)
    y1.append(0)
    y2.append(0)
    y3.append(0)
    y4.append(0)
    y5.append(0)
    y6.append(0)
    y7.append(0)
    y8.append(0)
    y9.append(0)
    y10.append(0)
    y11.append(0)
    y12.append(0)
layout = [
        [sg.Button('ГНС', enable_events=True, key='-B1-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Слив', enable_events=True, key='-B2-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Разот', enable_events=True, key='-B3-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Ргсп', enable_events=True, key='-B4-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Рслив', enable_events=True, key='-B5-', font='Helvetica 16', size=(15, 1)),
         sg.Button('t масла', enable_events=True, key='-B6-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Рнагн', enable_events=True, key='-B7-', font='Helvetica 16', size=(15, 1))],
        [sg.Button('Авар. откл.', enable_events=True, key='-B8-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Откр. ограждение', enable_events=True, key='-B9-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Откр. дверь каб.', enable_events=True, key='-B10-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Ремни', enable_events=True, key='-B11-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Трап отведен', enable_events=True, key='-B12-', font='Helvetica 16', size=(15, 1)),
         sg.Button('Трап подведен', enable_events=True, key='-B13-', font='Helvetica 16', size=(15, 1)),
         sg.Button('СП готова', enable_events=True, key='-B14-', font='Helvetica 16', size=(15, 1))],
        [sg.Canvas(size=(4, 3), key='-CANVAS-', pad=(40, 30))],
        [sg.Button('Exit', font='Helvetica 16', size=(10, 1))],
        ]
window = sg.Window('State Display', layout, size=(1920, 1280), finalize=True)
plt.ioff()
plt.figure(figsize=(18, 8), dpi=100)
fig1 = plt.gcf()
figure_x, figure_y, figure_w, figure_h = fig1.bbox.bounds
canvas_elem = window['-CANVAS-'].TKCanvas
canvas_elem.Size = (int(figure_w), int(figure_h))
ax1 = new_subplot(3, 2, 1, fig1)
ax2 = new_subplot(3, 2, 3, fig1)
ax3 = new_subplot(3, 2, 5, fig1)
ax4 = new_subplot(3, 2, 2, fig1)
ax5 = new_subplot(3, 2, 4, fig1)
ax6 = new_subplot(3, 2, 6, fig1)
fig1.subplots_adjust(hspace=0.3)
fig1.suptitle('Current HC state', fontsize=16)
plt.tight_layout()
[line1] = ax1.plot(x, y1, 'b')
[line2] = ax1.plot(x, y2, 'r')
[line3] = ax2.plot(x, y3, 'b')
[line4] = ax2.plot(x, y4, 'r')
[line5] = ax3.plot(x, y5, 'b')
[line6] = ax3.plot(x, y6, 'r')
[line7] = ax4.plot(x, y7, 'b')
[line8] = ax4.plot(x, y8, 'r')
[line9] = ax5.plot(x, y9, 'b')
[line10] = ax5.plot(x, y10, 'r')
[line11] = ax6.plot(x, y11, 'b')
[line12] = ax6.plot(x, y12, 'r')
timing = time.time()
ani = animation.FuncAnimation(fig1, update_animation,  fargs=(timing, x, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12), interval=10)
fig_agg = draw_figure(canvas_elem, fig1)
plt.show()
udp_socket.close()