-3
import os
import sys
from typing import List
from scipy import signal
from scipy import interpolate
from scipy.interpolate import interp1d
import numpy as np
import csv
import itertools
from string import ascii_uppercase
import time
import matplotlib.pyplot as plt
import scipy.io as sio
import wvfreader
import math
from scipy.fft import fft, ifft, fftfreq
import xlsxwriter
import datetime
import PySimpleGUI as sg

Those are the imports of my program. I plot then show the graph with plt.show() but after closing the graph window (pressing 'x' on the top right), the program still remains in the plt.show() command and will not go further. Does the plt.show() run into some kind of loop?

I tried different methods of plotting but nothing works. The program works without the plt.show() perfectly. Even when I save the plots instead of showing up the program works. So, the main problem seems to be plt.show(). Are there some other possibilities to show the plot instead of using plt.show()?

Michael S.
  • 3,050
  • 4
  • 19
  • 34
  • 4
    It's nothing to do with imports show complete code Janik – Bhargav - Retarded Skills Aug 03 '23 at 07:12
  • I've seen this exact question 2 (?) days ago but last time there was code associated with it. Did you delete your account and make a new one just to ask this question again? The same issue persists between this question and the last one, you are not showing the relevant code to help fix your problem. – Michael S. Aug 03 '23 at 14:34
  • Last time you mentioned thinking it had to do with PysimpleGUI but even then you did not show your PysimpleGUI code. I can almost guarantee that the issue lies in your `for-loop` for PysimpleGUI and not having some kind of exit condition. – Michael S. Aug 03 '23 at 14:37

1 Answers1

2

Because I'm not sure what your code is or what you're trying to do and this is the second time you've asked this question (on a new account) maybe a new approach is needed. Here, I am showing an example of how to create a GUI for your graph and how the graph can change every time you click on the graph button. Perhaps this technique will better serve what you want to accomplish. I highly recommend you read through the PySimpleGUI documentation:

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

# Random data for the graph
data1 = [1,2,3,4,5]
data2 = [5,4,3,2,1]

# Graphing function
def graph(data):
    fig, ax1 = plt.subplots(1)
    plt.plot(data)
    draw_figure(window['Graph_Canvas'].TKCanvas, fig)
    
# This definition is used to create a Matplotlib figure inside the main window
def draw_figure(canvas, fig):
    if canvas.children:
        plt.close()                           # close any open figures (too many open pyplot figures eat up RAM)
        for child in canvas.winfo_children(): # close any already created canvas figures
            child.destroy()            
    figure_canvas_agg = FigureCanvasTkAgg(fig, canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
    return figure_canvas_agg

# Layout of the GUI
layout = [
    [sg.Push(), sg.B('Graph'), sg.Push()],
    [sg.Canvas(key = 'Graph_Canvas')],
]

window = sg.Window(title='Test', layout = layout, resizable=True).Finalize()
window.BringToFront()

# Some place holder variables to be used later
version, data = 0, []
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
        
    elif event == 'Graph':
        
        # If data1 was graphed then graph data2 and vice versa
        if version == 0: 
            data = data1
            version = 1
        else:
            data = data2
            version = 0
        # Call the graph function
        graph(data)
        
window.close()
Michael S.
  • 3,050
  • 4
  • 19
  • 34