I'd like to save/pickle a matplotlib figure containing a matplotlib.widgets.Widget object to send it to a colleague without him having to run the code I use to generate it.
I know since matplotlib version 1.2 matplotlib.figure.Figure are pickable. However, when the figure contains a matplotlib.widgets.Widget, pickle fails to save it.
diff --git i/lib/matplotlib/widgets.py w/lib/matplotlib/widgets.py
index 16e03f5624..a9f1145bf2 100644
--- i/lib/matplotlib/widgets.py+++ w/lib/matplotlib/widgets.py@@ -115,9 +115,10 @@ class AxesWidget(Widget):
def __init__(self, ax):
self.ax = ax
- self.canvas = ax.figure.canvas
self._cids = []
+ canvas = property(lambda self: self.ax.figure.canvas)+
def connect_event(self, event, callback):
"""
Connect a callback function with an event.
"fixes" the pickling itself (and seems like a reasonable patch regardless), but even then, the pickling process will drop any connected callbacks as functions can generally not be pickled.
@anntzer is the dropping of callbacks when picking a figure builtin matplotlib ? or is it a limitation of the standard pickle module ? or is there some kind of criterion that make a function pickle-able or not ?
I am in the same kind of situation as @LucaAmerio, where I create matplotlib figure (with Qt backend) and callbacks connected to the figure and/or the axes. I'd like to be able to save and re-open those figure with the callbacks still active
The ability to pickle functions is limited per https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled. We could consider pickling only whatever can actually be pickled and drop the rest, but I believe dropping everything (and asking whoever does the unpickling to reconnect things as needed) is not unreasonable either.
changed the title [-][ENH]: pickle (or save) matplotlib figure with insteractive slider[/-][+][ENH]: pickle (or save) matplotlib figure with insteractive slider[/+]on Aug 12, 2024
Activity
anntzer commented on Feb 17, 2022
"fixes" the pickling itself (and seems like a reasonable patch regardless), but even then, the pickling process will drop any connected callbacks as functions can generally not be pickled.
mocquin commented on Jun 20, 2024
@anntzer is the dropping of callbacks when picking a figure builtin matplotlib ? or is it a limitation of the standard pickle module ? or is there some kind of criterion that make a function pickle-able or not ?
I am in the same kind of situation as @LucaAmerio, where I create matplotlib figure (with Qt backend) and callbacks connected to the figure and/or the axes. I'd like to be able to save and re-open those figure with the callbacks still active
anntzer commented on Jun 20, 2024
The ability to pickle functions is limited per https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled. We could consider pickling only whatever can actually be pickled and drop the rest, but I believe dropping everything (and asking whoever does the unpickling to reconnect things as needed) is not unreasonable either.