MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Matplotlib_Animated.py:21–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19 return figure_canvas_agg
20
21def main():
22
23 NUM_DATAPOINTS = 10000
24 # define the form layout
25 layout = [[sg.Text('Animated Matplotlib', size=(40, 1),
26 justification='center', font='Helvetica 20')],
27 [sg.Canvas(size=(640, 480), key='-CANVAS-')],
28 [sg.Text('Progress through the data')],
29 [sg.Slider(range=(0, NUM_DATAPOINTS), size=(60, 10),
30 orientation='h', key='-SLIDER-')],
31 [sg.Text('Number of data points to display on screen')],
32 [sg.Slider(range=(10, 500), default_value=40, size=(40, 10),
33 orientation='h', key='-SLIDER-DATAPOINTS-')],
34 [sg.Button('Exit', size=(10, 1), pad=((280, 0), 3), font='Helvetica 14')]]
35
36 # create the form and show it without the plot
37 window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',
38 layout, finalize=True)
39
40 canvas_elem = window['-CANVAS-']
41 slider_elem = window['-SLIDER-']
42 canvas = canvas_elem.TKCanvas
43
44 # draw the initial plot in the window
45 fig = Figure()
46 ax = fig.add_subplot(111)
47 ax.set_xlabel("X axis")
48 ax.set_ylabel("Y axis")
49 ax.grid()
50 fig_agg = draw_figure(canvas, fig)
51 # make a bunch of random data points
52 dpts = [randint(0, 10) for x in range(NUM_DATAPOINTS)]
53
54 for i in range(len(dpts)):
55
56 event, values = window.read(timeout=10)
57 if event in ('Exit', None):
58 exit(69)
59 slider_elem.update(i) # slider shows "progress" through the data points
60 ax.cla() # clear the subplot
61 ax.grid() # draw the grid
62 data_points = int(values['-SLIDER-DATAPOINTS-']) # draw this many data points (on next line)
63 ax.plot(range(data_points), dpts[i:i+data_points], color='purple')
64 fig_agg.draw()
65
66 window.close()
67
68if __name__ == '__main__':
69 main()

Callers 1

Calls 6

readMethod · 0.95
closeMethod · 0.95
draw_figureFunction · 0.70
updateMethod · 0.45
plotMethod · 0.45
drawMethod · 0.45

Tested by

no test coverage detected