成功解决import matplotlib; matplotlib.use('Agg') # pylint: disable=multiple-statements

成功解决import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements


解决问题

import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements
The backend was *originally* set to 'module://ipykernel.pylab.backend_inline' by the following code:
  File "f:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "f:\program files\python\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "F:\Program Files\Python\Python36\Lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
    app.start()
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\kernelapp.py", line 486, in start
    self.io_loop.start()
  File "F:\Program Files\Python\Python36\Lib\site-packages\tornado\platform\asyncio.py", line 127, in start
    self.asyncio_loop.run_forever()
  File "f:\program files\python\python36\lib\asyncio\base_events.py", line 421, in run_forever
    self._run_once()
  File "f:\program files\python\python36\lib\asyncio\base_events.py", line 1431, in _run_once
    handle._run()
  File "f:\program files\python\python36\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "F:\Program Files\Python\Python36\Lib\site-packages\tornado\platform\asyncio.py", line 117, in _handle_events
    handler_func(fileobj, events)
  File "F:\Program Files\Python\Python36\Lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "F:\Program Files\Python\Python36\Lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "F:\Program Files\Python\Python36\Lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "F:\Program Files\Python\Python36\Lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "F:\Program Files\Python\Python36\Lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "F:\Program Files\Python\Python36\Lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 2909, in run_ast_nodes
    if self.run_code(code, result):
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-0f0a85121700>", line 2, in <module>
    get_ipython().run_line_magic('matplotlib', 'inline')
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 2131, in run_line_magic
    result = fn(*args,**kwargs)
  File "<decorator-gen-108>", line 2, in matplotlib
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\magic.py", line 187, in <lambda>
    call = lambda f, *a, **k: f(*a, **k)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\magics\pylab.py", line 99, in matplotlib
    gui, backend = self.shell.enable_matplotlib(args.gui)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\interactiveshell.py", line 3051, in enable_matplotlib
    pt.activate_matplotlib(backend)
  File "F:\Program Files\Python\Python36\Lib\site-packages\IPython\core\pylabtools.py", line 311, in activate_matplotlib
    matplotlib.pyplot.switch_backend(backend)
  File "F:\Program Files\Python\Python36\Lib\site-packages\matplotlib\pyplot.py", line 231, in switch_backend
    matplotlib.use(newbackend, warn=False, force=True)
  File "F:\Program Files\Python\Python36\Lib\site-packages\matplotlib\__init__.py", line 1410, in use
    reload(sys.modules['matplotlib.backends'])
  File "f:\program files\python\python36\lib\importlib\__init__.py", line 166, in reload
    _bootstrap._exec(spec, module)
  File "F:\Program Files\Python\Python36\Lib\site-packages\matplotlib\backends\__init__.py", line 16, in <module>
    line for line in traceback.format_stack()

  import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements

解决思路

原因之一,在最新的一个matplotlib版本中,此导入的方式不对。
有网友说还可能存在其他原因,如有其他解决办法,欢迎网友留言!

解决方法

尝试,将

from matplotlib import pyplot as plt

改为

import matplotlib.pyplot as plt

(0)

相关推荐