Skip to content

Figure is unpicklable after savefig #6870

Description

@Kojoley
import matplotlib.pyplot as plt
import pickle
from io import BytesIO

def test_pickle_after_savefig():
    fig = plt.figure()
    fig.add_subplot(111)
    for fmt in ['pdf', 'png', 'svg']:
        fig.savefig(BytesIO(), format=fmt)
        try:
            pickle.dump(fig, BytesIO())
        except:
            print('Unpickable after "%s"' % fmt)
            raise

test_pickle_after_savefig()
>python test_pickle_after_savefig.py
Unpickable after "svg"
Traceback (most recent call last):
  File "test_pickle_after_savefig.py", line 16, in <module>
    test_pickle_after_savefig()
  File "test_pickle_after_savefig.py", line 11, in test_pickle_after_savefig
    pickle.dump(fig, BytesIO())
TypeError: cannot serialize '_io.TextIOWrapper' object

I think those warnings from pytest are maybe related:

>py.test -n 4 lib/matplotlib/tests/test_backend_svg.py::test_fill_black_with_alpha
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: C:\Working\Repositories\matplotlib, inifile: pytest.ini
plugins: xdist-1.14
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling
.sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>

With a file name pickle fails in the pdf backend (and svg too):

import matplotlib.pylab as plt
import pickle
from io import BytesIO

def test_pickle_after_savefig2():
    fig = plt.figure()
    fig.add_subplot(111)
    for fmt in ['pdf', 'png', 'svg']:
        fig.savefig('test.%s' % fmt, format=fmt)
        try:
            pickle.dump(fig, BytesIO())
        except:
            print('Unpickable format is "%s"' % fmt)
            raise

test_pickle_after_savefig2()
>python test_pickle_after_savefig2.py
Unpickable format is "pdf"
Traceback (most recent call last):
  File "test_pickle_after_savefig2.py", line 16, in <module>
    test_pickle_after_savefig()
  File "test_pickle_after_savefig2.py", line 11, in test_pickle_after_savefig
    pickle.dump(fig, BytesIO())
TypeError: cannot serialize '_io.BufferedWriter' object

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions