python - 谷歌 Colabs Notebook 中保存的图像与本地计算机中保存的图像不同

标签 python matplotlib google-colaboratory python-3.7

我使用 matplotlib 创建了一个图形,并使用 savefig 函数保存它。 以下是我使用的代码,

import matplotlib.pyplot as plt

x = [0, 2, 4, 6]
y = [1, 3, 4, 8]

plt.plot(x,y)
plt.xticks([]), plt.yticks([])
plt.box(False)
plt.savefig('plot.png')
plt.show()

当我在 google colabs 中运行以下代码时,它会生成尺寸为 432 x 288 的图像 当我在本地计算机中运行相同的代码时,它将生成尺寸为640 x 480

的图像

所以我尝试通过添加以下更改来调整在本地计算机中生成的图像的大小

plt.figure(figsize=(4.32, 2.88))

使用以下代码验证两个图像是否完全相同,但它们不相同

import PIL.Image, PIL.ImageChops
im1 = PIL.Image.open(colab_image.png') 
im2 = PIL.Image.open(local_image.png')
if list(im1.getdata()) == list(im2.getdata()): 
    print("Identical");
else:
    print("Different");

colab我的本地机器环境都有 -> python 3.7.0 64位,matplotlib == 3.2.2

请帮我解决这个问题。因为,我不知道为什么会发生这种情况。我们将非常感谢您的帮助。

这是 link对于colab笔记本

谢谢并致以最诚挚的问候。

最佳答案

我认为可能存在操作系统兼容性问题。请分别在Windows和Linux/Mac环境下检查代码。

关于python - 谷歌 Colabs Notebook 中保存的图像与本地计算机中保存的图像不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67441933/

相关文章:

Python:如何使用从列计算的乘法字段扩展 DataFrame

python - 如何从列表中随机选择一个变量,然后在 python 中修改它?

python - 根据信号所在位置调整 xrange

python - Matplotlib:在 PolarAxes 上绘制一系列径向线

google-colaboratory - XLNetTokenizer 需要 SentencePiece 库,但在您的环境中找不到

bash - 在Google Colab中构建Docker容器: unknown flag -t

python - 如何将数据保存到文件而不是字典列表

python - scikit-learn PCA 转换返回不正确的缩减特征长度

python - 气泡图标题和颜色

google-colaboratory - 如何限制 Google Colaboratory 中的单元格文本输出?