'Open maplotlib window with PySimpleGUI
I am very new to PySimpleGUI and I am getting stuck on a certain part of my code. I have some data of which I want to make a plot using matplotlib and I can get it to be opened in a canvas in the PySimpleGUI window but what I would like to achieve is for the figure to be plotted in a separate window (preferably the matplotlib window) so that it becomes easy to save the figure on my computer.
I was wondering if there is a simple way of doing it. Essentially what I want to obtain is to press a button 'Plot' in PySimpleGUI and then obtain my plot in a window such as this:
My code is currently quite large and not very optimized (it only works for the specific dataset I am using for now) so if I need to share the code I have please let me know and I can create a simple code as an example. If someone could explain to me how I can achieve my goal directly that would be greatly appreciated.
Thank you!
Solution 1:[1]
Just plt your figure and call plt.show(block=False) under if event == 'Plot': in your event loop.
import PySimpleGUI as sg
import matplotlib.pyplot as plt
year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
unemployment_rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
def create_plot(year, unemployment_rate):
plt.plot(year, unemployment_rate, color='red', marker='o')
plt.title('Unemployment Rate Vs Year', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.ylabel('Unemployment Rate', fontsize=14)
plt.grid(True)
return
layout = [[sg.Button('Show')]]
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
elif event == 'Show':
create_plot(year, unemployment_rate)
plt.show(block=False)
window.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason Yang |
