0

Currently I'm plotting my data in a figure that shows in a separate window that opens when I click my submit button to plot my data. However, I want to plot the data straight into what I have outlined with a canvas instead. I haven't used python much, so I don't know how to utilize the FigureCanvasTkAgg to my advantage here. Any help would be appreciated. Here is the code snippet:

def set_data(driver, slice, lap_num):
    if slice == "Fastest Lap":
        self = driver.best_tel
    
    elif slice == "Specific Lap":
        lap_n = driver.ses[driver.ses['LapNumber'] == lap_num]
        lap_n_tel = lap_n.get_car_data().add_distance()
        self = lap_n_tel

    elif slice == "Full Session":
        self = driver.ses

    return self

def make_figure():
        fig = plt.figure(1, figsize=(16,9), constrained_layout=True)
        ax = fig.subplots()
        return fig, ax

def plot_ax(driver, data, fig, xvar, yvar, ax):
    ax.plot(data[xvar], data[yvar], color=driver.teamcolor, label=f'{driver.id}')
    ax.set_xlim(data[xvar].min(), data[xvar].max())
 

def compare(grandprix, driver, slice, xvar, yvar, fig, ax, lap_num):
    for abb in driver:
        driver = Driver(grandprix, abb)
        data = set_data(driver, slice, lap_num)
        plot_ax(driver, data, fig, xvar, yvar, ax)


def set_title(grandprix, driver, yvar, slice, ses, lap_num, comp):
    global title
    if slice == "Specific Lap":
        analysis = f"Lap {lap_num}, {yvar} \n {grandprix.event.year} {grandprix.event['EventName']}, {ses}"

    elif slice != "Specific Lap":
        analysis = f"{slice}, {yvar} \n {grandprix.event.year} {grandprix.event['EventName']}, {ses}" 
    if comp == True:
        title = analysis

    elif comp != True:
        title = f"{driver.bio['FullName']} " + analysis
    plt.suptitle(f"{title}")

def design_plot(ax, xvar, yvar):
        ax.set_xlabel(xvar)
        ax.set_ylabel(yvar)

        ax.minorticks_on()
        ax.grid(visible=True, axis='both', which='major', linewidth=0.9, alpha=0.3)
        ax.grid(visible=True, axis='both', which='minor', linestyle=':', linewidth=0.6, alpha=0.3)

        ax.legend()

def show_plot():
    plt.show()

def analyse():
    global year, gp, ses, abb, slice
    global lap_num, xvar, yvar, comp

    year = int(values['-YEAR-'])
    gp = values['-GP-'][0]
    ses = values['-SESSION-']
    abb = values['-DRIVER-']
    slice = values['-SLICE-']
    lap_num = int(values['-LAPNUM-'])
    xvar = values['-DRIVERXVAR-']
    yvar = values['-DRIVERYVAR-']
    comp = values['-COMPARE-']

    fig, ax = make_figure()

    if comp == True:
        compare(grandprix, abb, slice, xvar, yvar, fig, ax, lap_num)
        driver = Driver(grandprix, abb[0])

    elif comp != True:
        driver = Driver(grandprix, abb[0])
        data = set_data(driver, slice, lap_num)
        plot_ax(driver, data, fig, xvar, yvar, ax)
        
    design_plot(ax, xvar, yvar)
    set_title(grandprix, driver, yvar, slice, ses, lap_num, comp)


    show_plot()




def make_window():
    sg.theme('DarkBlue14')
    lists = Lists()
    menu_def = [[('&Help'), ['&About', '&GitHub', 'E&xit']]]

    header_layout = [[sg.Image(source='src/common/images/icon_header.png', size=(120, 60), expand_x=True, expand_y=True)],]

. 
    menu_layout_column = [
            [sg.Menubar(menu_def, key='-MENU-')],
            [sg.Frame('', header_layout, size=(500,100), key='-HEAD-')],
            [sg.OptionMenu(lists.Years.list, default_value=f'{lists.Years.list[-1]}', expand_x=True, key='-YEAR-')],
            [sg.Button('Load Season', size=(10,1), expand_x=True)],
            [sg.Listbox(lists.GrandPrix.list, enable_events=True, expand_x=True, size=(None,10), select_mode='single', horizontal_scroll=False, visible=False, pad=(7,7,7,7), key='-GP-')],
            [sg.OptionMenu(lists.Sessions.list, default_value=f'Select Session...', expand_x=True, visible=False, key='-SESSION-')],
            [sg.Button('Drivers in Session', visible=False, expand_x=True, key='-LOADDRIVERS-')],
            [sg.Listbox(lists.Drivers.list, enable_events=True, expand_x=True, size=(None,10), select_mode='single', horizontal_scroll=False, visible=False, pad=(7,7,7,7), key='-DRIVER-')],
            [sg.Checkbox('Compare drivers?', enable_events=True, visible=False, key='-COMPARE-')],
            [sg.OptionMenu(lists.SessionSlice.list, default_value=f'Evalutate Full Session?', disabled=True, expand_x=True, visible=False, key='-SLICE-')],
            [sg.Text('Enter Lap Number', visible=False, key='-LAPASK-')],
            [sg.Input(default_text= 0, expand_x=True, visible=False, key='-LAPNUM-')],
            [sg.Button('Select Telemetry', visible=False, disabled=True, expand_x=True, key='-LOADVARS-')],
            [sg.OptionMenu(lists.LapVarsY.list, default_value='Y Variable...', expand_x=True, visible=False, key='-DRIVERYVAR-')],
            [sg.OptionMenu(lists.LapVarsX.list, default_value='X Variable...', expand_x=True, visible=False, key='-DRIVERXVAR-')],
            [sg.Button('Confirm All', visible=True, expand_x=True, key='-CONFIRM ALL-')],
            [sg.Button('Submit', visible=True, disabled=True, expand_x=True, key='-PLOT-')],
            ]


    graph_layout_column = [

    ]

    layout = [
        [sg.Column(menu_layout_column),
        sg.VSeparator(),
        sg.Column(graph_layout_column)],
                ]


    window = sg.Window('Gr1dGuru', layout, margins=(10, 10), finalize=True)

    window.set_min_size(window.size)
    
    return window

I have tried messing around with the FigureCanvasTkAgg with a fixed plot, but when I try modifying anything with my function setup I'm completely stuck.

Krissa
  • 1
  • There're lot of scripts, `Demo_Matplotlib_...py`, to demo how to embedded matplotlib in PySimpleGUI, the link https://github.com/PySimpleGUI/PySimpleGUI/tree/master/DemoPrograms – Jason Yang Dec 20 '22 at 01:29

0 Answers0