Issue
Problem: My plot appears on the canvas inside the window, but also a separate window figure gets created with the plot. I would like remove that extra window, since it seems that it is taking resources and time and maybe makes the system occasionally to stop responding.
Help would be appreciated, here is a sample of my code that reproduces the problem.
Code sample:
import matplotlib.pyplot as plt
from os import path
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
matplotlib.use("TkAgg")
import pickle
from numpy import arange
import PySimpleGUI as sg
def draw_figure_w_toolbar(canvas, fig, canvas_toolbar):
if canvas.children:
for child in canvas.winfo_children():
child.destroy()
if canvas_toolbar.children:
for child in canvas_toolbar.winfo_children():
child.destroy()
figure_canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
figure_canvas_agg.draw()
toolbar = Toolbar(figure_canvas_agg, canvas_toolbar)
toolbar.update()
figure_canvas_agg.get_tk_widget().pack(side='right', fill='both', expand=1)
class Toolbar(NavigationToolbar2Tk):
def __init__(self, *args, **kwargs):
super(Toolbar, self).__init__(*args, **kwargs)
data_folder = ''
filename = 'plot_data.p'
[t0,p0, t_filt, p_filt, band,t,p] = pickle.load(open(path.join(data_folder, path.basename(filename)),'rb'))
plot_column = [
[
sg.Button(enable_events=True, key="Plot",button_text="Plot")],
[sg.T('Controls:')],
[sg.Canvas(key='controls_cv')],
[sg.T('Figure:')],
[sg.Column(
layout=[
[sg.Canvas(key='canvas',
# it's important that you set this size
size=(400 * 2, 600)
)]
],
background_color='#DAE0E6',
pad=(0, 0)
)],]
layout_Main_window = [
[
sg.Column(plot_column),
]
]
window = sg.Window("Click detection", layout_Main_window, finalize=True, location=(30, 80))
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
# --------- Data folder ---------
elif event == "Plot":
fig = plt.figure(figsize=(11, 10))
plt.subplot(2,1,1)
plt.title('Original signals')
plt.plot(t0, p0,'.', label='Original signal', zorder = 0)
plt.plot(t_filt, p_filt, label='filtered ['+str(band[0])+','+str(band[1])+']', zorder = 5)
plt.legend(loc='best')
plt.ylabel('Intensity')
x_axis1 = [round(min(min(t0),min(t_filt)),1), round(max(max(t0),max(t_filt)),1)]
plt.gca().set_xlim(x_axis1)
plt.grid()
plt.subplot(2,1,2)
plt.title('Pen + equipment signal')
plt.plot(t, p, color='#ff7f0e', label='clicking period')
plt.legend(loc='best')
plt.ylabel('relative intensity')
plt.gca().set_xlim(x_axis1)
plt.xticks(arange(x_axis1[0], x_axis1[1], 0.5))
plt.xlabel('time (s)')
plt.grid()
plot_name = path.join(data_folder, path.basename(filename.replace('.p', '.png')))
plt.savefig(data_folder)
draw_figure_w_toolbar(window['canvas'].TKCanvas, fig, window['controls_cv'].TKCanvas)
Solution
fig = plt.figure(figsize=(11, 10))
should be changed to:
plt.ioff()
plt.figure(figsize=(11, 10))
fig = plt.gcf()
Answered By - CameFromSpace
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.