2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2018-09-06 20:20:37 +00:00
|
|
|
import PySimpleGUI as sg
|
2019-09-19 17:32:25 +00:00
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from numpy.random import rand
|
|
|
|
|
|
|
|
def draw_figure(canvas, figure):
|
|
|
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
|
|
|
figure_canvas_agg.draw()
|
|
|
|
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
|
|
|
return figure_canvas_agg
|
|
|
|
|
2018-08-29 14:28:03 +00:00
|
|
|
def main():
|
|
|
|
# define the form layout
|
2018-09-06 20:20:37 +00:00
|
|
|
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
2019-09-19 17:32:25 +00:00
|
|
|
[sg.Canvas(size=(640, 480), key='-CANVAS-')],
|
|
|
|
[sg.Button('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
2018-08-29 14:28:03 +00:00
|
|
|
|
|
|
|
# create the form and show it without the plot
|
2019-09-19 17:32:25 +00:00
|
|
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, finalize=True)
|
2018-08-29 14:28:03 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
canvas_elem = window['-CANVAS-']
|
2018-08-29 14:28:03 +00:00
|
|
|
canvas = canvas_elem.TKCanvas
|
2019-09-19 17:32:25 +00:00
|
|
|
# draw the intitial scatter plot
|
|
|
|
fig, ax = plt.subplots()
|
|
|
|
ax.grid(True)
|
|
|
|
fig_agg = draw_figure(canvas, fig)
|
2018-08-29 14:28:03 +00:00
|
|
|
|
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read(timeout=10)
|
2019-06-26 15:09:42 +00:00
|
|
|
if event in ('Exit', None):
|
2018-08-29 14:28:03 +00:00
|
|
|
exit(69)
|
|
|
|
|
2019-09-19 17:32:25 +00:00
|
|
|
ax.cla()
|
|
|
|
ax.grid(True)
|
|
|
|
for color in ['red', 'green', 'blue']:
|
|
|
|
n = 750
|
|
|
|
x, y = rand(2, n)
|
|
|
|
scale = 200.0 * rand(n)
|
|
|
|
ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none')
|
|
|
|
ax.legend()
|
|
|
|
fig_agg.draw()
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
2018-08-29 14:28:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|