【问题标题】:Pysimple GUI not clearing matplotlib graph canvasPysimple GUI 未清除 matplotlib 图形画布
【发布时间】:2020-10-29 20:26:27
【问题描述】:

上下文:之前发布了类似的 q,现在我必须使用单选按钮而不是功能列表。原因是当我输入我的文件进行处理时,图形的输入变量将在此时定义,而不是在代码的前面,所以唯一的方法是使用单选按钮,这样我就可以添加函数输入参数在这里。

问题:我的问题是,当我单击单选按钮时,之前的图表没有被清除,并且新图表打印在原始图表下方。请有人帮忙 - 谢谢!

此处的最小代码示例:

import PySimpleGUI as sg
import time
import os
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.ticker import NullFormatter  
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
sg.theme('Dark')

def PyplotSimple():
    import numpy as np
    import matplotlib.pyplot as plt
    t = np.arange(0., 5., 0.2)          

    plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')

    fig = plt.gcf()  # get the figure to show
    return fig

def PyplotSimple2():
    import numpy as np
    import matplotlib.pyplot as plt
    t = np.arange(0., 5., 0.2)        
    plt.plot(t, t, 'r--', t, t ** 2, 'b--', t, t ** 3, 'b--')

    fig = plt.gcf()  # get the figure to show
    return fig

def draw_plot():
    plt.plot([0.1, 0.2, 0.5, 0.7])
    fig = plt.gcf()  # get the figure to show
    return fig



def draw_figure(canvas, figure):
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
    return figure_canvas_agg


def delete_figure_agg(figure_agg):
    figure_agg.get_tk_widget().forget()
    plt.close('all')


layout= [
    [sg.Text('my GUI', size=(40,1),justification='c', font=("Arial 10"))],
    [sg.Text('Browse to file:'), sg.Input(size=(40,1), key='input'),sg.FileBrowse (key='filebrowse')],

    [sg.Button('Process' ,bind_return_key=True), 
     sg.Radio('1',key= 'RADIO1',group_id='1', enable_events = True,default=False, size=(10,1)),
          sg.Radio('2', key= 'RADIO2',group_id='1',enable_events = True, default=False, size=(10,1)),
           sg.Radio('3', key='RADIO3',group_id='1',enable_events = True, default=False, size=(12,1))],

    [sg.Canvas(size=(200,200), background_color='white',key='-CANVAS-')],
    [sg.Exit()]] 


window = sg.Window('my gui', layout, grab_anywhere=False, finalize=True)
#window.Maximize()
figure_agg = None
# The GUI Event Loop

while True:
    event, values = window.read()
    #print(event, values)                  # helps greatly when debugging
    if event in (sg.WIN_CLOSED, 'Exit'):             # if user closed window or clicked Exit button
        break
          
    if figure_agg:
        delete_figure_agg(figure_agg)
        figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)
    if event == 'Process':
        #my function here 
        #the output of this function will decide the inputs to the graph which is why i need to use radio buttons
        sg.popup('Complete - view graphs',button_color=('#ffffff','#797979'))
    
    if event ==  'RADIO1':
        fig= PyplotSimple()
        figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)

    if event ==  'RADIO2':
        fig= PyplotSimple2()
        figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)
        
    
    if event ==  'RADIO3':
        fig= draw_plot()
        figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)
  
    
    elif event == 'Exit':
        break

window.close()

【问题讨论】:

    标签: python python-3.x matplotlib pysimplegui


    【解决方案1】:

    在绘制新图形或轴之前,您可能需要使用cla()clf() 方法来清除图形。在 Matplotlib 中找到它们。

    在函数PyplotSimplePyplotSimple2 中不必进行以下导入。

        import numpy as np
        import matplotlib.pyplot as plt
    

    在这里,您在删除 figure_agg 后再次创建它。 删除注释标记的行。

        if figure_agg:
            delete_figure_agg(figure_agg)
            #figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)
    

    其实

    • 在调用 draw_figure 时在 PSG 画布上添加了另一个画布
    • 调用delete_figure_agg时不删除画布,只是在包中忘记了

    这是另一个示例,展示了我如何在 PSG 画布上使用 matplotlib 图形。

    import math
    
    from matplotlib import use as use_agg
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    import matplotlib.pyplot as plt
    
    import PySimpleGUI as sg
    
    # Use Tkinter Agg
    use_agg('TkAgg')
    
    # PySimplGUI window
    layout = [[sg.Graph((640, 480), (0, 0), (640, 480), key='Graph')]]
    window = sg.Window('Matplotlib', layout, finalize=True)
    
    # Default settings for matplotlib graphics
    fig, ax = plt.subplots()
    
    # Link matplotlib to PySimpleGUI Graph
    canvas = FigureCanvasTkAgg(fig, window['Graph'].Widget)
    plot_widget = canvas.get_tk_widget()
    plot_widget.grid(row=0, column=0)
    
    theta = 0   # offset angle for each sine curve
    while True:
    
        event, values = window.read(timeout=10)
    
        if event == sg.WINDOW_CLOSED:
            break
    
        # Generate points for sine curve.
        x = [degree for degree in range(1080)]
        y = [math.sin((degree+theta)/180*math.pi) for degree in range(1080)]
    
        # Reset ax
        ax.cla()
        ax.set_title("Sensor Data")
        ax.set_xlabel("X axis")
        ax.set_ylabel("Y axis")
        ax.set_xscale('log')
        ax.grid()
    
        plt.plot(x, y)      # Plot new curve
        fig.canvas.draw()   # Draw curve really
    
        theta = (theta + 10) % 360  # change offset angle for curve shift on Graph
    
    window.close()
    

    你可以在这里找到只调用一次FigureCanvasTkAgg 或类似FigureCanvasTkAgg 的东西,这里没有调用delete_figure_agg,而只是调用了ax.cla() 来清除数字。

    【讨论】:

    • 感谢我删除了它们。我正在关注这个演示:github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/… - 在我添加单选按钮之前,一切正常 - 你知道我可能做错了什么吗?
    • 更新如上。
    • 非常感谢!只是想知道为什么我也会得到一个弹出图?所以我的 GUI 上有一个,图形的第二个副本作为弹出窗口?
    • 实际上,每次调用draw_figure 时,都会在PySimpleGUI Canvas 上创建一个新画布,调用delete_figure_agg 并没有删除该画布,而是将其从包中取出。更新如上。
    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2021-02-02
    • 2016-07-03
    • 2013-03-16
    相关资源
    最近更新 更多