saving figures using plt.savefig on colaboratory
As mentioned in another answer, the files.download
function is the perfect solution if you want to create the image file and download it on the fly. But what if you do not actually need to download the file, but you simply want to store the image to a directory in your Google Drive account? Maybe you are generating tons of such files (e.g. intermediate results during a time-consuming machine learning job) and you just can't download each file one by one.
In that case the solution I employed might be of help for you too. First of all, let's mount our Google Drive on our runtime.
# mount drive
from google.colab import drive
drive.mount('/content/gdrive')
Note: you can do that at the beginning of your notebook and then forget about it for the entire session, no need to do that for each image of course!
With the Google Drive mounted, you can now store your image files (or any other file you wish, for that matter) in any directory of your choice in Drive, e.g.:
images_dir = '/content/gdrive/My Drive/Images'
plt.savefig(f"{images_dir}/abc.png")
maybe it can save the picture independently
from google.colab import files
plt.savefig("abc.png")
files.download("abc.png")
https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb&scrollTo=p2E4EKhCWEC5