Loading matplotlib pickle figure in a tkinter frame -
i have small interface loads data matplotlib figure. able load pickles contain made plots somehow cannot make work.
this 1 of examples matplotlib embed plot within tkinter:
import sys import pickle import matplotlib numpy import arange, sin, pi, cos matplotlib.backends.backend_tkagg import figurecanvastkagg, navigationtoolbar2tkagg matplotlib.backend_bases import key_press_handler matplotlib.figure import figure if sys.version_info[0] < 3: import tkinter tk else: import tkinter tk def restore_from_pickle(pickle_address, fig): fig.clear() open(pickle_address, 'rb') fid: fig = pickle.load(fid) return def _quit(): root.quit() root.destroy() def on_key_event(event): print('you pressed %s' % event.key) key_press_handler(event, canvas, toolbar) matplotlib.use('tkagg') root = tk.tk() root.wm_title("embedding in tk") f = figure(figsize=(5, 4), dpi=100) t = arange(0.0, 3.0, 0.01) #plot store = f.add_subplot(111) s = sin(2*pi*t) a.plot(t, s) #saving pickle open('figure_pickle', 'wb') fid: pickle.dump(f, fid) #initial plot display f.clear() = f.add_subplot(111) s = cos(2*pi*t) a.plot(t, s) # tk.drawingarea canvas = figurecanvastkagg(f, master=root) canvas.show() canvas.get_tk_widget().pack(side=tk.top, fill=tk.both, expand=1) toolbar = navigationtoolbar2tkagg(canvas, root) toolbar.update() canvas._tkcanvas.pack(side=tk.top, fill=tk.both, expand=1) canvas.mpl_connect('key_press_event', on_key_event) button = tk.button(master=root, text='f(sin)', command=restore_from_pickle('figure_pickle', f)) #button = tk.button(master=root, text='quit', command=_quit) button.pack(side=tk.bottom) tk.mainloop()
this produces like:
if press button closes window. modified saves pickle containing 1 figure , button loads image. however, when run it:
the original plot not drawn , neither 1 in pickle...
what happening?
thanks advice
Comments
Post a Comment