pythonbeginner
pythonbeginner

Reputation: 63

How to save plot to google drive?

I am working with google colab and I am trying to save an image to google drive.

# Set the figure size to (5, 5)
with rc_context({'figure.figsize': (5, 5)}):
  # Generate the UMAP plot
  sc.pl.umap(adata, color='type')
  # Save the plot to Google Drive   
  plt.savefig('/content/drive/umap_plot.png')

However, the file that is generated on the google drive is empty. I also tried to save it asplt.savefig ('/content/umap_plot.png', dpi=300) and as plt.savefig('/content/drive/umap_plot.pdf') but all the files turn out empty. How can I fix this?

Thank you

Upvotes: 0

Views: 669

Answers (1)

KEIRSTEN C
KEIRSTEN C

Reputation: 96

Have you mounted your Google Drive in the Colab environment? By default, the Google Colab environment is isolated from your Google Drive, so you need to establish a connection between them to read and write files. I have tried to make an example with what you provided but the path may be different than what you have.

from google.colab import drive
drive.mount('/content/drive')

with rc_context({'figure.figsize': (5, 5)}):
    sc.pl.umap(adata, color='type')
    plt.savefig('/content/drive/MyDrive/umap_plot.png', dpi=300)


plt.savefig('/content/drive/MyDrive/umap_plot.pdf', dpi=300)
plt.savefig('/content/drive/MyDrive/umap_plot.svg', dpi=300)

Upvotes: 0

Related Questions