You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi I am using matplotlib 2.0.0b4 in ubuntu 15.10 and I wonder if anyone could please offer me some advice on how to tackle this issue:
There are several examples on the gallery on how to embed matplotlib figures in a GUI and how to use the pickle pickle functionality however I cannot make them both work together. This is one example from matplotlib 1.5.3:
import sys
import pickle
import matplotlib
from numpy import arange, sin, pi, cos
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
def restore_from_pickle(pickle_address, fig):
fig.clear()
with open(pickle_address, 'rb') as 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 to store
a = f.add_subplot(111)
s = sin(2*pi*t)
a.plot(t, s)
#Saving to pickle
with open('figure_pickle', 'wb') as fid:
pickle.dump(f, fid)
#Initial plot display
f.clear()
a = f.add_subplot(111)
s = cos(2*pi*t)
a.plot(t, s)
# a 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()
The original code generates a GUI interface with a quit button:
The code above just changes this button to display a different figure previously stored in a pickle file. However, when I run it, the initial plot does not display and the one in the pickle is not loaded either.
I do not think persisting figures as pickles is a good idea, it is better to persist your data and then have a function which creates the figure you need
defmy_plotting_func(fig_or_ax, *args, **kwargs):
# do some stuff
...
defcreate_from_file(fig_or_ax, fname):
data_payload=from_disk(fname)
returnmy_plotting_func(fig_or_ax, *data_payload['args'], **data_payload['kwargs'])
with data_payload being a dictionary that looks something like
data_payload= {'plot_name': 'my_plot', # to do dispatch if you want'args': tuple(...),
'kwargs': dict(..)}
Please follow up about this question on the user mailing list. It will be seen by a larger audience and we try to keep the issue tracker for feature requests / bug reports rather than usage questions.
Hi I am using matplotlib 2.0.0b4 in ubuntu 15.10 and I wonder if anyone could please offer me some advice on how to tackle this issue:
There are several examples on the gallery on how to embed matplotlib figures in a GUI and how to use the pickle pickle functionality however I cannot make them both work together. This is one example from matplotlib 1.5.3:
The original code generates a GUI interface with a quit button:
https://www.dropbox.com/s/qqpprcfpiix5n45/original_code.png?dl=0
The code above just changes this button to display a different figure previously stored in a pickle file. However, when I run it, the initial plot does not display and the one in the pickle is not loaded either.
https://www.dropbox.com/s/lm889um1p13jr22/modified_code.png?dl=0
Any advice or any documentation which could help me would be most welcome.
The text was updated successfully, but these errors were encountered: