在pysimplegui中使用matplotlib进行绘图

kwvwclae  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(175)

如何在使用PySimpleGui中的matplotlib再次绘图之前清理先前的绘图?
我想画一个条形图,我可以画,但不能删除使用以下代码:

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import PySimpleGUI as sg
import random

layout=[[sg.B('Draw'),sg.B('Delete')],[sg.Canvas(key='canvas1')]]

window=sg.Window("Test", layout)

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

def drawBars(x_data,y_data,barColor,legend):
    plt.cla()
    p1 = plt.bar(x_data, y_data, width=0.9, color=barColor)
    plt.legend((p1[0],), (legend,))
    plt.tight_layout()
    fig = plt.gcf()
    return fig

l=range(1,10)
y_data=list(l)
x_data=list(random.sample(l,len(l)))

while True:
    event, value = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Draw':
        draw_figure(window['canvas1'].TKCanvas, drawBars(x_data,y_data,'red','plot'))
    if event == 'Delete':
        pass
        #what code i should here to do the job?
window.close()

有没有什么函数,我可以调用清理任何以前的图纸?有人可以请帮助我吗,我一直在谷歌上寻找它真的真的很长时间,所有我发现的是一些相关的代码[1],并没有很好地与我合作,我找不到一个简单和干净的。谢谢大家。
[1][https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_Browser.py](https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_Browser.py)

xmjla07d

xmjla07d1#

我把你的代码放进OpenAI的Codex,这是我得到的结果:
编号

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import PySimpleGUI as sg
import random

layout=[[sg.B('Draw'),sg.B('Delete')],[sg.Canvas(key='canvas1')]]

window=sg.Window("Test", layout)

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

def drawBars(x_data,y_data,barColor,legend):
    plt.cla()
    p1 = plt.bar(x_data, y_data, width=0.9, color=barColor)
    plt.legend((p1[0],), (legend,))
    plt.tight_layout()
    fig = plt.gcf()
    return fig

l=range(1,10)
y_data=list(l)
x_data=list(random.sample(l,len(l)))

figure_canvas_agg = None

while True:
    event, value = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Draw':
        if figure_canvas_agg is not None:
            figure_canvas_agg.clear()
        figure_canvas_agg = draw_figure(window['canvas1'].TKCanvas, drawBars(x_data,y_data,'red','plot'))
    if event == 'Delete':
        if figure_canvas_agg is not None:
            figure_canvas_agg.clear()
window.close()

变更说明

1.添加了全局变量figure_canvas_agg以存储FigureCanvasTkAgg对象。
1.在Draw事件中,检查figure_canvas_agg是否不是None。如果不是None,请呼叫FigureCanvasTkAgg对象的clear方法。
1.在Delete事件中,检查figure_canvas_agg是否不是None。如果不是None,请呼叫FigureCanvasTkAgg对象的clear方法。

相关问题