在 Jupyter Notebook 中导入 pyplot
Importing pyplot in a Jupyter Notebook
运行 Python 2.7 并试图让绘图工作教程推荐以下命令。
from matplotlib import pyplot as plt
当从命令行运行
时工作正常
python -c "from matplotlib import pyplot as plt"
但我在 Jupyter Notebook 中尝试 运行 时遇到错误。
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-21-1d1446f6fa64> in <module>()
----> 1 from matplotlib import pyplot as plt
/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py in <module>()
112
113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
115
116 _IP_REGISTERED = None
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.pyc in pylab_setup()
30 # imports. 0 means only perform absolute imports.
31 backend_mod = __import__(backend_name,
---> 32 globals(),locals(),[backend_name],0)
33
34 # Things we pull in from all backends
/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in <module>()
154 configure_inline_support(ip, backend)
155
--> 156 _enable_matplotlib_integration()
/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in _enable_matplotlib_integration()
152 backend = get_backend()
153 if ip and backend == 'module://%s' % __name__:
--> 154 configure_inline_support(ip, backend)
155
156 _enable_matplotlib_integration()
/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in configure_inline_support(shell, backend)
359 except ImportError:
360 return
--> 361 from matplotlib import pyplot
362
363 cfg = InlineBackend.instance(parent=shell)
ImportError: cannot import name pyplot
以下命令有效
import matplotlib
但是下面给了我类似的错误
import matplotlib.pyplot
您也可以使用 %matplotlib inline
魔法,但必须在它前面加上纯 %matplotlib
行:
作品(新图window)
%matplotlib
import matplotlib.pyplot as plt
作品(内联数字)
%matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
不起作用
%matplotlib inline
import matplotlib.pyplot as plt
另外: 似乎是同一个问题。它看起来像是最近在 ipykernel 中引入的错误。也许有人将这个或其他问题标记为重复,谢谢。
运行 Python 2.7 并试图让绘图工作教程推荐以下命令。
from matplotlib import pyplot as plt
当从命令行运行
时工作正常python -c "from matplotlib import pyplot as plt"
但我在 Jupyter Notebook 中尝试 运行 时遇到错误。
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-21-1d1446f6fa64> in <module>()
----> 1 from matplotlib import pyplot as plt
/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py in <module>()
112
113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
115
116 _IP_REGISTERED = None
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.pyc in pylab_setup()
30 # imports. 0 means only perform absolute imports.
31 backend_mod = __import__(backend_name,
---> 32 globals(),locals(),[backend_name],0)
33
34 # Things we pull in from all backends
/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in <module>()
154 configure_inline_support(ip, backend)
155
--> 156 _enable_matplotlib_integration()
/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in _enable_matplotlib_integration()
152 backend = get_backend()
153 if ip and backend == 'module://%s' % __name__:
--> 154 configure_inline_support(ip, backend)
155
156 _enable_matplotlib_integration()
/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in configure_inline_support(shell, backend)
359 except ImportError:
360 return
--> 361 from matplotlib import pyplot
362
363 cfg = InlineBackend.instance(parent=shell)
ImportError: cannot import name pyplot
以下命令有效
import matplotlib
但是下面给了我类似的错误
import matplotlib.pyplot
您也可以使用 %matplotlib inline
魔法,但必须在它前面加上纯 %matplotlib
行:
作品(新图window)
%matplotlib
import matplotlib.pyplot as plt
作品(内联数字)
%matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
不起作用
%matplotlib inline
import matplotlib.pyplot as plt
另外: