|
| 1 | +""" |
| 2 | +Matplotlib Demo |
| 3 | +
|
| 4 | +Show some simple plots using matplotlib and the TkAgg backend. |
| 5 | +The program will quit after 5 seconds. |
| 6 | +
|
| 7 | +""" |
| 8 | +import __main__; print(__main__.__doc__) |
| 9 | + |
| 10 | + |
| 11 | +import tkthread |
| 12 | +root = tkthread.tkinstall(ensure_root=True) |
| 13 | + |
| 14 | + |
| 15 | +import matplotlib as mpl |
| 16 | +mpl.rcParams['backend'] = 'TkAgg' |
| 17 | + |
| 18 | +import matplotlib.pyplot as plt |
| 19 | +import numpy as np |
| 20 | + |
| 21 | + |
| 22 | +# set up a function that is invoked on the main thread |
| 23 | +@tkthread.called_on_main |
| 24 | +def main_call(): |
| 25 | + x = np.linspace(0, 10, 1000) |
| 26 | + y = np.sin(x) |
| 27 | + ax2.plot(x, y, color='blue') |
| 28 | + ax2.set_title(repr(threading.current_thread())) |
| 29 | + |
| 30 | + |
| 31 | +def thread_call(): |
| 32 | + print('starting a plot from: ', threading.current_thread()) |
| 33 | + global fig, ax1, ax2 |
| 34 | + plt.ion() |
| 35 | + x = np.linspace(0, 10, 1000) |
| 36 | + y = np.sin(x) |
| 37 | + fig = plt.figure(2) |
| 38 | + ax1, ax2 = fig.subplots(2, 1) |
| 39 | + ax1.plot(x, y, color='red') |
| 40 | + ax1.set_title(repr(threading.current_thread())) |
| 41 | + main_call() |
| 42 | + print('finished') |
| 43 | + |
| 44 | +import threading |
| 45 | +threading.Thread(target=thread_call, daemon=True).start() |
| 46 | + |
| 47 | +print('starting main loop, quit in 5 seconds') |
| 48 | +root.after(5000, root.quit) |
| 49 | +root.mainloop() |
0 commit comments