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:
JavaScript
x
39
39
1
import matplotlib.pyplot as plt
2
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
3
import PySimpleGUI as sg
4
import random
5
6
layout=[[sg.B('Draw'),sg.B('Delete')],[sg.Canvas(key='canvas1')]]
7
8
window=sg.Window("Test", layout)
9
10
def draw_figure(canvas, figure):
11
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
12
figure_canvas_agg.get_tk_widget().forget()
13
figure_canvas_agg.draw()
14
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
15
return figure_canvas_agg
16
17
def drawBars(x_data,y_data,barColor,legend):
18
plt.cla()
19
p1 = plt.bar(x_data, y_data, width=0.9, color=barColor)
20
plt.legend((p1[0],), (legend,))
21
plt.tight_layout()
22
fig = plt.gcf()
23
return fig
24
25
l=range(1,10)
26
y_data=list(l)
27
x_data=list(random.sample(l,len(l)))
28
29
while True:
30
event, value = window.read()
31
if event == sg.WIN_CLOSED:
32
break
33
if event == 'Draw':
34
draw_figure(window['canvas1'].TKCanvas, drawBars(x_data,y_data,'red','plot'))
35
if event == 'Delete':
36
pass
37
#what code i should here to do the job?
38
window.close()
39
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
JavaScript
1
43
43
1
import matplotlib.pyplot as plt
2
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
3
import PySimpleGUI as sg
4
import random
5
6
layout=[[sg.B('Draw'),sg.B('Delete')],[sg.Canvas(key='canvas1')]]
7
8
window=sg.Window("Test", layout)
9
10
def draw_figure(canvas, figure):
11
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
12
figure_canvas_agg.get_tk_widget().forget()
13
figure_canvas_agg.draw()
14
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
15
return figure_canvas_agg
16
17
def drawBars(x_data,y_data,barColor,legend):
18
plt.cla()
19
p1 = plt.bar(x_data, y_data, width=0.9, color=barColor)
20
plt.legend((p1[0],), (legend,))
21
plt.tight_layout()
22
fig = plt.gcf()
23
return fig
24
25
l=range(1,10)
26
y_data=list(l)
27
x_data=list(random.sample(l,len(l)))
28
29
figure_canvas_agg = None
30
31
while True:
32
event, value = window.read()
33
if event == sg.WIN_CLOSED:
34
break
35
if event == 'Draw':
36
if figure_canvas_agg is not None:
37
figure_canvas_agg.clear()
38
figure_canvas_agg = draw_figure(window['canvas1'].TKCanvas, drawBars(x_data,y_data,'red','plot'))
39
if event == 'Delete':
40
if figure_canvas_agg is not None:
41
figure_canvas_agg.clear()
42
window.close()
43
Explanation of changes
- Added a global variable
figure_canvas_agg
to store theFigureCanvasTkAgg
object. - In the
Draw
event, check if thefigure_canvas_agg
is notNone
. If it is notNone
, call theclear
method of theFigureCanvasTkAgg
object. - In the
Delete
event, check if thefigure_canvas_agg
is notNone
. If it is notNone
, call theclear
method of theFigureCanvasTkAgg
object.