pythonplotserializationnetflix-metaflow

How to store and retrieve figures as artifacts in MetaFlow?


How do I save a plot as a data artifact in MetaFlow? Plotting libraries usually have you write out to a file on disk. How do I view the figure afterwards?


Solution

  • The trick is to serialize the figure's bytes as an artifact:

        from io import BytesIO
    
        buf = BytesIO()
        fig.savefig(buf)
        self.plot = buf.getvalue()
    

    Then, you can fetch the artifact from the run object and directly use the bytes to plot:

    from IPython.display import Image
    
    Image(data=run.data.plot)
    

    Source: https://github.com/outerbounds/dsbook/blob/main/chapter-6/forecast1.py#L21 and @ville on MetaFlow's slack