Search code examples
pythonmatplotlibseaborn

How to save and then load a seaborn jointplot


I have a seaborn jointplot

fig = sns.jointplot(x=[1,2,3,4],y=[4,3,2,1])

How do I save it and then load it back as an image inside databricks so I can use it in a subplot?

when I try to load the image back in from dbfs using mping.imread it tells me 'Jointgrid does not have an attribute imread'


Solution

  • I was able to get the mentioned sample jointplot to save down and load back in using this code

    Saving:

    plot = sns.jointplot(x=[1,2,3,4],y=[4,3,2,1])
    plot.savefig('/dbfs/FileStore/figure.png')
    

    Loading:

    img = plt.imread('/dbfs/FileStore/figure.png')
    

    Displaying:

    plt.imshow(img)
    

    Hope this helped!

    Edit: Additionally I know you mentioned making it part of a subplot you can use this code to do that:

    f, axarr = plt.subplots(2,1,figsize=(25,16))
    axarr[1,1].imshow(mpimg.imread(figname1))
    axarr[2,1].imshow(mpimg.imread(figname2))
    [ax.set_axis_off() for ax in axarr.ravel()]
    plt.tight_layout()
    plt.show()