Visitar URL original
[Bug]: Unable to pickle figure with draggable legend · Issue #25300 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20230227213224/https://github.com/matplotlib/matplotlib/issues/25300
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

[Bug]: Unable to pickle figure with draggable legend #25300

Closed
inxp opened this issue Feb 23, 2023 · 3 comments · Fixed by #25311
Closed

[Bug]: Unable to pickle figure with draggable legend #25300

inxp opened this issue Feb 23, 2023 · 3 comments · Fixed by #25311
Labels
topic: pickle Issues to do with pickling
Milestone

Comments

@inxp
Copy link

inxp commented Feb 23, 2023 •

Bug summary

I am unable to pickle figure with draggable legend. Same error comes for draggable annotations.

Code for reproduction

import matplotlib.pyplot as plt
import pickle

fig = plt.figure()
ax = fig.add_subplot(111)

time=[0,1,2,3,4]
speed=[40,43,45,47,48]

ax.plot(time,speed,label="speed")

leg=ax.legend()
leg.set_draggable(True) #pickling works after removing this line 

pickle.dumps(fig)
plt.show()

Actual outcome

TypeError: cannot pickle 'FigureCanvasQTAgg' object

Expected outcome

Pickling successful

Additional information

No response

Operating system

Windows 10

Matplotlib Version

3.7.0

Matplotlib Backend

No response

Python version

3.10

Jupyter version

No response

Installation

pip

@inxp inxp changed the title Unable to pickle figure with draggable legend [Bug:] Unable to pickle figure with draggable legend Feb 23, 2023
@inxp inxp changed the title [Bug:] Unable to pickle figure with draggable legend [Bug]: Unable to pickle figure with draggable legend Feb 23, 2023
@inxp
Copy link
Author

inxp commented Feb 24, 2023

#25311 fixes it. Thanks for the quick response :)

@inxp
Copy link
Author

inxp commented Feb 25, 2023

Update

Consider the following example

import sys
from PySide6.QtWidgets import QWidget, QApplication, QVBoxLayout
from PySide6.QtGui import QKeySequence, QShortcut

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import random
import pickle

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.figure = Figure()

        data = [random.random() for _ in range(10)]

        ax = self.figure.add_subplot(111)
        ax.plot(data, '*-',label='RandomData')
        legend = ax.legend()
        
        self.canvas = FigureCanvas(self.figure)
        self.canvas.draw()

        legend.set_draggable(True,use_blit=True)

        pd = QShortcut(QKeySequence(self.tr("Ctrl+C")), self)
        pd.activated.connect(self.pickle_now)

        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)

    def pickle_now(self):
        pickle.dumps(self.canvas.figure)
        print('pickling success')


    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Window()
    main.show()
    sys.exit(app.exec_())

Pickling works when legend is not dragged. After dragging when I try to pickle I get the error TypeError: cannot pickle 'matplotlib.backends._backend_agg.BufferRegion' object.

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

anntzer commented Feb 26, 2023

Reposting #25311 (comment): we should move the implementation of BufferRegion from C to Python (essentially making it an opaque type holding a numpy array and x1/x2/y1/y2 coords), which will make it picklable and also may help moving towards #23882. I don't think it's particularly hard to do on the engineering side, just a fair amount of boring work with C extensions...

@QuLogic QuLogic added this to the v3.7.1 milestone Feb 27, 2023
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