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

Error when pickling an object obtained by draw() #729

Closed
lucazav opened this issue Nov 25, 2023 · 3 comments
Closed

Error when pickling an object obtained by draw() #729

lucazav opened this issue Nov 25, 2023 · 3 comments

Comments

@lucazav
Copy link

lucazav commented Nov 25, 2023

I created a plot in the following way:

import os
import pickle
import pandas as pd
from plotnine import (
    options, theme_tufte, ggplot, aes, geom_bar,
    geom_text, after_stat, labs
)

dataset_url = 'http://bit.ly/titanic-dataset-csv'
df = pd.read_csv(dataset_url)

options.dpi = 300

p = (
        ggplot(df) 
        + aes(x='Pclass', fill="factor(Pclass)") 
        + geom_bar()
        + geom_text(
            aes(label = after_stat('count')),
            stat = 'count',
            nudge_x = -0.14,
            nudge_y = 0.125,
            va = 'bottom',
            size = 8
        )
        + geom_text(
            aes(label = after_stat('prop*100'), group=1),
            stat = 'count',
            nudge_x = 0.14,
            nudge_y = 0.125,
            va = 'bottom',
            format_string = '({:.1f}%)',
            size = 8
        )
        + labs(title='Passenger Count by Class',
               x='Class', y='Count', fill='Class')
        + theme_tufte()
)

Now I'm trying to pickle the related Matplotlib figure as follows:

mplt = p.draw()

project_folder = r'<my-folder>'

pickle.dump( mplt, open(os.path.join(project_folder, "mplt.pkl"), "wb") )

But I'm getting the following error:

NotImplementedError: Sorry, pickling not yet supported. See pydata/patsy#26 if you want to help.

I tried to pickle a plot generated directly with Matplotlib and everything works fine.

What's wrong with the Matplotlib figure given by the ggplot draw() method?

@lucazav
Copy link
Author

lucazav commented Jan 14, 2024

@has2k1 I installed the developer version of plotnine in my conda environment on Windows using:

pip install "plotnine @ https://github.com/has2k1/plotnine/archive/master.zip"

Then I tried the upon code and I'm getting the following error:

cannot pickle 'KeyedRef' object

Is there anything wrong in my installation?

@has2k1
Copy link
Owner

has2k1 commented Jan 14, 2024

This works for me.

from plotnine import *
from plotnine.data import mtcars

def test_pickle(obj):
    import io
    import pickle
    
    with io.BytesIO() as f:
        pickle.dump(obj, f)
        f.seek(0)
        unpickled_obj = pickle.load(f)
        
    return unpickled_obj

p  = (
 ggplot(mtcars, aes("wt", "mpg"))
 + geom_point()
)

fig = p.draw()

test_pickle(fig)
test_pickle(p)

@lucazav
Copy link
Author

lucazav commented Jan 14, 2024

My fault, I was using dill instead of pickle. Now it's working like a charm with pickle.

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

No branches or pull requests

2 participants