Skip to content
Advertisement

Drawing using matplotlib in pysimplegui

How can i clean up previous drawing before drawing again with matplotlib in PySimpleGui?

I wanted to draw a bar chart which i could be able to draw but not able to delete using the following code:

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()

Are there any functions that i can call to clean up any previous drawings? Can someone please helpme with it, i was looking for it for really really long time in google, all i found are some related codes [1] that didn’t work well with me, i couldn’t find a simple and clean one. Thank you guys.

[1]https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_Browser.py

Advertisement

Answer

I put your code into OpenAI’s Codex, and this is what I got back:

Code

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()

Explanation of changes

  1. Added a global variable figure_canvas_agg to store the FigureCanvasTkAgg object.
  2. In the Draw event, check if the figure_canvas_agg is not None. If it is not None, call the clear method of the FigureCanvasTkAgg object.
  3. In the Delete event, check if the figure_canvas_agg is not None. If it is not None, call the clear method of the FigureCanvasTkAgg object.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement