gpt4 book ai didi

python - Matplotlib:将图形另存为 iPython 笔记本中的文件

转载 作者:IT老高 更新时间:2023-10-28 20:34:01 24 4
gpt4 key购买 nike

我正在尝试将 Matplotlib 图保存为 iPython 笔记本中的文件。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
ax.plot([1,2])

fig.savefig('test.png')

iPython notebook 中的内联 View 看起来不错:

Inline view of figure in iPython notebook


文件“test.png”几乎是空的。绘图似乎移到了右上角,您可以在角落看到刻度标签“1.0”和“0.0”。

test.png

如何从 iPython 笔记本生成一个看起来像内联 View 的文件?

最佳答案

问题已解决:将 'bbox_inches='tight' 参数添加到 savefig。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.plot([1,2])

savefig('test.png', bbox_inches='tight')

我不明白这里发生了什么,但该文件现在看起来像 iPython 笔记本内联文件。耶。

关于python - Matplotlib:将图形另存为 iPython 笔记本中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19271309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com