gpt4 book ai didi

python - 在其他导入与 pep8 冲突之前需要 matplotlib.use。忽略还是修复?

转载 作者:太空狗 更新时间:2023-10-29 22:10:41 25 4
gpt4 key购买 nike

我有一个像这样开始的 pythonscript:

#!/usr/bin/env python
import matplotlib
matplotlib.use("Agg")

from matplotlib.dates import strpdate2num
import numpy as np
import pylab as pl
from cmath import rect, phase

它就像一个魅力,但我的编辑提示:E402 模块级导入不在文件顶部 [pep8]

如果我将 matplotlib.use("Agg") 向下移动,脚本将无法运行。

我应该忽略这个错误吗?或者有什么办法可以解决这个问题?

编辑: 我知道 PEP8 说这只是一个建议,它可能会被忽略,但我希望有一个很好的方法来初始化模块而不违反 PEP8 指南,因为我不认为我可以让我的编辑器在每个文件的基础上忽略这条规则。

EDIT2:我正在使用带有 linter-pylama 的 Atom

最佳答案

显然,matplotlib 现在有一个 switch_backend() 函数:

import matplotlib.pyplot
# import other modules
matplotlib.pyplot.switch_backend('Agg')

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.switch_backend

但要小心,你有爆炸的危险:

Switch the default backend. This feature is experimental, and is only expected to work switching to an image backend. e.g., if you have a bunch of PostScript scripts that you want to run from an interactive ipython session, you may want to switch to the PS backend before running them to avoid having a bunch of GUI windows popup. If you try to interactively switch from one GUI backend to another, you will explode.

Calling this command will close all open windows.

它适用于 matplotlib 1.3.1,但不适用于 1.0.0。

关于python - 在其他导入与 pep8 冲突之前需要 matplotlib.use。忽略还是修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305810/

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