matplotlib - 在 Google Colaboratory 上保存或下载 matplotlib 绘图图像

标签 matplotlib google-colaboratory

我在 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/

相关文章:

python - 从 Pandas 数据框制作多个饼图(每行一个)

python - seaborn distplot中的y轴是什么?

jupyter - 创建指向 Google Colab jupyter 笔记本中动态生成的文件的文件下载链接

python - 如何在 Matplotlib 中连接图形坐标?

python-3.x - 使用 np.polyfit 沿多项式回归生成的曲线计算和绘制切线

python - Pandas 并排堆积条形图

python - Colab 上的 Tensorflow Eager Execution

tensorflow - 在 Google Colab 中使用多个 GPU 在 Tensorflow 中进行分布式训练

python - 使用 matplotlib 时,LaTeX 方程不会在 google Colaboratory 中呈现

tensorflow - 微调 BERT 的最后 x 层