jupyter nbconvert不保存matplotlib.savefig的文件

问题描述 投票:2回答:1

我正在尝试使用pyplot.savefig函数保存png文件。通过jupyter web UI运行时工作正常。但是当我通过命令行运行它时,它不会创建图像。

命令行:jupyter nbconvert FILE_NAME.ipynb

码:

import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')

plt.savefig('test-image-2.png', format='png', dpi=1200)

plt.show()

我在这做错了什么?

python matplotlib
1个回答
1
投票

您需要将命令更改为:

jupyter nbconvert FILE_NAME.ipynb --execute

否则,没有任何单元格被执行。

这在jupyter nbconvert --help显示的文档中说明:

$ jupyter nbconvert --help
This application is used to convert notebook files (*.ipynb) to various other
formats.

WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

--debug
    set log level to logging.DEBUG (maximize logging output)
--generate-config
    generate default config file
-y
    Answer yes to any questions instead of prompting.
--execute
    Execute the notebook prior to export.
[snip]
© www.soinside.com 2019 - 2024. All rights reserved.