Jupyter中运行魔法%matplotlib inline出错ModuleNotFoundError: No module named ‘matplotlib‘

目录

代码如下:

错误提示如下

​ 解决办法


 代码如下:

## (1)导入所需要的库
# -*- coding : utf-8 -*-
import numpy as np
%matplotlib inline
from matplotlib import pyplot as plt

## (2)生成随机数据x及目标y
# 设置随机种子,生成同一份数据,方便多种方法比较
np.random.seed(100)
x = np.linspace(-1,1,100).reshape(100,1)
y = 3*np.power(x,2) + 2 + 0.2*np.random.rand(x.size).reshape(100,1)

## (3)查看x,y数据分布情况
plt.scatter(x,y)
plt.show()

错误提示如下

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-9-ebc254f17b7d> in <module>
      4 from IPython import get_ipython
      5 
----> 6 get_ipython().run_line_magic('matplotlib', 'inline')
      7 from matplotlib import pyplot as plt
      8 

D:\softinstall\Anaconda\envs\pytorch\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2315                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2316             with self.builtin_trap:
-> 2317                 result = fn(*args, **kwargs)
   2318             return result
   2319 

<decorator-gen-109> in matplotlib(self, line)

D:\softinstall\Anaconda\envs\pytorch\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

D:\softinstall\Anaconda\envs\pytorch\lib\site-packages\IPython\core\magics\pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

D:\softinstall\Anaconda\envs\pytorch\lib\site-packages\IPython\core\interactiveshell.py in enable_matplotlib(self, gui)
   3405         """
   3406         from IPython.core import pylabtools as pt
-> 3407         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3408 
   3409         if gui != 'inline':

D:\softinstall\Anaconda\envs\pytorch\lib\site-packages\IPython\core\pylabtools.py in find_gui_and_backend(gui, gui_select)
    278     """
    279 
--> 280     import matplotlib
    281 
    282     if gui and gui != 'auto':

ModuleNotFoundError: No module named 'matplotlib'

 解决办法

 最后在运行

猜你喜欢

转载自blog.csdn.net/qq_15698613/article/details/107690696