我在 Google Colaboratory 中使用 matplotlib 并尝试将绘图图像保存在某处(本地下载或上传到 Google Drive)。我目前正在内嵌显示图像:
plt.show()
这是我尝试过的,虽然它只下载一个空白图像:
import os
local_download_path = os.path.expanduser('~/data')
plot_filepath = os.path.join(local_download_path, "plot.png")
plt.savefig(plot_filepath)
from google.colab import files
files.download(plot_filepath)
我也尝试使用 Drive API 上传绘图图像,但也没有成功。
最佳答案
在使用 savefig()
之前,您是否确保没有显示绘图? ?这是一个完整的工作示例:
import matplotlib.pyplot as plt
from google.colab import files
test = plt.figure()
plt.plot([[1, 2, 3], [5, 2, 3]])
plt.savefig('test.pdf')
files.download('test.pdf')
关于matplotlib - 在 Google Colaboratory 上保存或下载 matplotlib 绘图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50953856/