2018-10-04 20:02:45 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-10-04 20:02:45 +00:00
|
|
|
import matplotlib
|
2019-11-25 06:04:49 +00:00
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
|
|
# matplotlib.use('TkAgg')
|
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
2018-10-04 20:02:45 +00:00
|
|
|
"""
|
|
|
|
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
|
|
|
|
|
|
|
Paste your Pyplot code into the section marked below.
|
|
|
|
|
|
|
|
Do all of your plotting as you normally would, but do NOT call plt.show().
|
|
|
|
Stop just short of calling plt.show() and let the GUI do the rest.
|
|
|
|
|
|
|
|
The remainder of the program will convert your plot and display it in the GUI.
|
|
|
|
If you want to change the GUI, make changes to the GUI portion marked below.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
|
|
|
|
|
|
|
|
values_to_plot = (20, 35, 30, 35, 27)
|
2018-10-08 17:30:33 +00:00
|
|
|
ind = np.arange(len(values_to_plot))
|
|
|
|
width = 0.4
|
2018-10-04 20:02:45 +00:00
|
|
|
|
|
|
|
p1 = plt.bar(ind, values_to_plot, width)
|
|
|
|
|
|
|
|
plt.ylabel('Y-Axis Values')
|
|
|
|
plt.title('Plot Title')
|
|
|
|
plt.xticks(ind, ('Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'))
|
|
|
|
plt.yticks(np.arange(0, 81, 10))
|
|
|
|
plt.legend((p1[0],), ('Data Group 1',))
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------- END OF YOUR MATPLOTLIB CODE -------------------------------
|
|
|
|
|
|
|
|
# ------------------------------- Beginning of Matplotlib helper code -----------------------
|
|
|
|
|
|
|
|
def draw_figure(canvas, figure, loc=(0, 0)):
|
2019-11-25 06:04:49 +00:00
|
|
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
2018-10-04 20:02:45 +00:00
|
|
|
figure_canvas_agg.draw()
|
2019-11-25 06:04:49 +00:00
|
|
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
|
|
|
return figure_canvas_agg
|
2018-10-04 20:02:45 +00:00
|
|
|
|
|
|
|
# ------------------------------- Beginning of GUI CODE -------------------------------
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('Light Brown 3')
|
2018-10-04 20:02:45 +00:00
|
|
|
|
|
|
|
fig = plt.gcf() # if using Pyplot then get the figure from the plot
|
|
|
|
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
|
|
|
|
|
|
|
# define the window layout
|
|
|
|
layout = [[sg.Text('Plot test', font='Any 18')],
|
2019-11-25 06:04:49 +00:00
|
|
|
[sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
|
2018-10-04 20:02:45 +00:00
|
|
|
[sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]
|
|
|
|
|
|
|
|
# create the form and show it without the plot
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',
|
|
|
|
layout, force_toplevel=True, finalize=True)
|
2018-10-04 20:02:45 +00:00
|
|
|
|
|
|
|
# add the plot to the window
|
2019-11-25 06:04:49 +00:00
|
|
|
fig_photo = draw_figure(window['-CANVAS-'].TKCanvas, fig)
|
2018-10-04 20:02:45 +00:00
|
|
|
|
|
|
|
# show it all again and get buttons
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
|
|
|
|
|
|
|
window.close()
|