Skip to content

[Bug]: Unable to pickle figure with aligned labels #25329

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
inxp opened this issue Feb 25, 2023 · 5 comments · Fixed by #25332
Closed

[Bug]: Unable to pickle figure with aligned labels #25329

inxp opened this issue Feb 25, 2023 · 5 comments · Fixed by #25332
Labels
topic: pickle Issues to do with pickling
Milestone

Comments

@inxp
Copy link

inxp commented Feb 25, 2023

Bug summary

Unable to pickle figure after calling align_labels()

Code for reproduction

import matplotlib.pyplot as plt
import pickle

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
time=[0,1,2,3,4]
speed=[40000,4300,4500,4700,4800]
acc=[10,11,12,13,14]
ax1.plot(time,speed)
ax1.set_ylabel('speed')
ax2.plot(time,acc)
ax2.set_ylabel('acc')

fig.align_labels() ##pickling works after removing this line 

pickle.dumps(fig)
plt.show()

Actual outcome

align.py", line 16
pickle.dumps(fig)
TypeError: cannot pickle 'weakref.ReferenceType' object

Expected outcome

Pickling successful

Additional information

No response

Operating system

Windows

Matplotlib Version

3.7.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@jklymak jklymak added the topic: pickle Issues to do with pickling label Feb 25, 2023
@jklymak
Copy link
Member

jklymak commented Feb 25, 2023

As you've noted, pickling is pretty fragile. Do you need to pickle?

@inxp
Copy link
Author

inxp commented Feb 26, 2023

I have embedded matplotlib in PySide6 and use it for plotting and analyzing time based 2-D data. This tool is used by people with no prior experience of python/matplotlib. Everything is done through UI. There is no scripting required to generate figures.

I was looking for a way to save the generated figures for future changes. So basically I want to pickle a figure object, compress it (size reduction+encryption) and then save it to disk. When reading the file back, I can decompress the file and then add figure object to canvas. I understand pickling is not recommended for saving figures due to security issues/matplotlib version mismatch etc issues. (as discussed in #15381). But there is no script used here to generate the figure.

I got some hope when issue #22482 was fixed. Hence I decided to raise other issues that I faced when pickling.

Thanks for all the help. specially @anntzer

@inxp
Copy link
Author

inxp commented Feb 26, 2023

Confirming #25332 fixes this issue.

@QuLogic QuLogic added this to the v3.8.0 milestone Feb 28, 2023
@tacaswell
Copy link
Member

As you've noted, pickling is pretty fragile. Do you need to pickle?

yes, we committed to Figures being pickable a while ago and I do not think we should back out of that.

The interactive stuff is on the edge and I think it is OK to say "you lose custom callback on un-pickle" because scope creep needs to be stopped, anything essentially static should work.

@tacaswell
Copy link
Member

If it is all in a UI, then the things the user can do are (very) limited, I suspect it would be possible to describe generating the figure as a series of steps that they did. Rather than using a pickle like this (you could argue that because you own both read/write it is ok-ish, but it still leaves an injection attach e.g. person B emails person A an "output" from your program which is malicious you are not actually getting around any of the security issues with pickle inherent with pickles) it would be better to write down the sequence of steps the user did and the replay those steps on load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: pickle Issues to do with pickling
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants