Discover this year's submissions to the Dataiku Frontrunner Awards and give kudos to your favorite use cases and success stories!READ MORE

Download or save matplotlib figure from notebook

Solved!
ConusAdonis
Level 1
Download or save matplotlib figure from notebook
I would like to download matplotlib figures I have created in a notebook to use them in communications. How can I do this. When I use savefig, I cannot find the resulting file anywhere.

This is a company installation so I do not have terminal access to the server's file system, so I need a way to download the figure or save them to folder I can access from DataIKU.

I noticed I can create static insights from matplotlib figures using dataIKU API, but these static insights cannot be downloaded either as far as I can see.
0 Kudos
1 Solution
UserBird
Dataiker
Dataiker

Hi,



To detail the solution provided by my coworker, here a more detailed example:



1° Create a file system folder in the flow, lets call it "plots"



2° in your jupyter notebook, adapt the following code:




import dataiku
import numpy as np
import matplotlib.pyplot as plt
import io
from dataiku.core.managed_folder import Folder

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

#plt.show()
bs = io.BytesIO()
plt.savefig(bs, format="png")
folder = Folder("plots")
folder.upload_stream("fig.png", bs.getvalue())


3° Come back to the flow, open the "plots" manager folder, fig.png should be present in it.



 



Cheers,



 



Mayeul



R&D

View solution in original post

4 Replies
UserBird
Dataiker
Dataiker

Hi, 



You can create a Folder in the flow and store your figures in it. Then you can download them easily. The API to manipulate Folder is here: https://doc.dataiku.com/dss/latest/python-api/managed_folders.html



Cheers,



Du Phan



R&D

0 Kudos
UserBird
Dataiker
Dataiker

Hi,



To detail the solution provided by my coworker, here a more detailed example:



1° Create a file system folder in the flow, lets call it "plots"



2° in your jupyter notebook, adapt the following code:




import dataiku
import numpy as np
import matplotlib.pyplot as plt
import io
from dataiku.core.managed_folder import Folder

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

#plt.show()
bs = io.BytesIO()
plt.savefig(bs, format="png")
folder = Folder("plots")
folder.upload_stream("fig.png", bs.getvalue())


3° Come back to the flow, open the "plots" manager folder, fig.png should be present in it.



 



Cheers,



 



Mayeul



R&D

ConusAdonis
Level 1
Author
works like a charm. Thanks.
0 Kudos
dshurgatw
Level 1

Matplotlib is a widely used Python library to plot graphs, plots, charts, etc. show() method is used to display graphs as output, but don't save it in any file. To save generated graphs in a file on a storage disk, small folding table savefig() method is used. savefig() : Save the current figure.

0 Kudos