Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading a matplotlib figure pickle within a tkinter GUI #7441

Closed
Vital-Fernandez opened this issue Nov 11, 2016 · 1 comment
Closed

Loading a matplotlib figure pickle within a tkinter GUI #7441

Vital-Fernandez opened this issue Nov 11, 2016 · 1 comment

Comments

@Vital-Fernandez
Copy link

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:

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.

@tacaswell
Copy link
Member

This is probably better sent to matplotlib-users@python.org (https://mail.python.org/mailman/listinfo/matplotlib-users you will need to register to post un-moderated)

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

def my_plotting_func(fig_or_ax, *args, **kwargs):
    # do some stuff
   ...

def create_from_file(fig_or_ax, fname):
    data_payload = from_disk(fname)
    return my_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.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants