Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Pickled Figure Loses sharedx Properties #4827

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

Closed
spaceluigi opened this issue Jul 30, 2015 · 4 comments
Closed

Pickled Figure Loses sharedx Properties #4827

spaceluigi opened this issue Jul 30, 2015 · 4 comments

Comments

@spaceluigi
Copy link

I am storing pickled Figures for plotting at later time but for some reason the sharedx property keeps getting lost on the 2nd (unpickled) figure. Execute the code below for a quick example. Is there a way to reset the shared x property? There is PR 1312 that talks about a potential unshare_x_axes function but there doesn't really appear to be a setting function either. This may actually be two problems though since the pickle should also maintain the sharedx property.

#!/usr/bin/env python
import cPickle as pickle
import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[0].set_title('Sharing X axis')
axarr[1].scatter(x, y)

pickle.dump(f,open('test.pickle','wb'))

new_plot = pickle.load(open('test.pickle','rb'))
new_plot.canvas.draw()

plt.show()
@spaceluigi spaceluigi changed the title Pickled Figure Loses sharedx and sharedy Properties Pickled Figure Loses sharedx Properties Jul 30, 2015
@freidrichen
Copy link

I'm also affected by this issue. Any progress or good workarounds?

@tacaswell tacaswell added this to the unassigned milestone Mar 2, 2016
@tacaswell
Copy link
Member

Sharex is implemented by holding a reference to the other Axes/Axis objects (I do not recall which off the top of my head). There obviously do not survive round tripping. The way to fix this would be to work on__setstate__ / __getstate__ of the Axes object to replace the Grouper data structure with a list of uids, to make sure that all of the Axes objects have those uids assigned to them, and then at the end of __getstate__ in Figure replace the uids back. To make this more fun, the Grouper objects are actually class-level attributes.

We do not officially encourage using pickle objects to serialize figures, the 'official' way is the function that you used to generate the figure.

@spaceluigi
Copy link
Author

I implemented a work around by pickling a custom python dictionary stuffed
with the plotting data and other plot info, e.g. title,xLabel,yLabel,
etc,. Then used custom plotting function that accepted the dictionary
object and re-created the plot.

On Wed, Mar 2, 2016, 2:19 PM Thomas A Caswell [email protected]
wrote:

Sharex is implemented by holding a reference to the other Axes/Axis
objects (I do not recall which off the top of my head). There obviously do
not survive round tripping. The way to fix this would be to work on
setstate / getstate of the Axes object to replace the Grouper
data structure with a list of uids, to make sure that all of the Axes
objects have those uids assigned to them, and then at the end of
getstate in Figure replace the uids back. To make this more fun,
the Grouper objects are actually class-level attributes.

We do not officially encourage using pickle objects to serialize figures,
the 'official' way is the function that you used to generate the figure.


Reply to this email directly or view it on GitHub
#4827 (comment)
.

@tacaswell
Copy link
Member

@lugi5720 That is a preferred way

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

Successfully merging a pull request may close this issue.

3 participants