Matplotlib导入不正确?

xxe27gdn  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(164)

尝试在VS Code中使用matplotlib。它要么没有正确导入,要么没有正常工作。

import matplotlib.pyplot as plt

plt.plot([1,2,3,4], [1,4,9,16])

plt.ylabel('y numbers')
plt.xlabel('x numbers')

plt.show()

终端显示:

PS C:\Users\peter\CodingProjects\graphing> & C:/Users/peter/AppData/Local/Microsoft/WindowsApps/python3.11.exe
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\__init__.py", line 144, in <module>
    from . import _api, _version, cbook, _docstring, rcsetup
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\rcsetup.py", line 27, in <module>  
    from matplotlib.colors import Colormap, is_color_like
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\colors.py", line 56, in <module>   
    from matplotlib import _api, _cm, cbook, scale
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\scale.py", line 22, in <module>    
    from matplotlib.ticker import (
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\ticker.py", line 138, in <module>  
    from matplotlib import transforms as mtransforms
  File "C:\Users\peter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\transforms.py", line 49, in <module>
    from matplotlib._path import (
ImportError: DLL load failed while importing _path: The specified module could not be found.
>>> plt.plot([1,2,3,4], [1,4,9,16])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'plt' is not defined
>>> plt.ylabel('y numbers')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'plt' is not defined
>>> plt.xlabel('x numbers')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'plt' is not defined
>>> plt.show()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'plt' is not defined

我用pip安装了matplotlib(看起来成功地安装到了我正在工作的项目中),把它作为plt导入到我的文件中,然后运行整个文件。但是它一直告诉我plt没有定义,还有沿着其他一些问题。
它看起来好像模块存在,但随后又表现得好像它不存在。
这是一个路径问题吗?
image of my code and entire terminal

gab6jxml

gab6jxml1#

在vscode终端上,运行

pip show matplotlib

这将显示Location属性

Location: /usr/lib/python3/dist-packages

如果您导航

cd /usr/lib

您可能会看到安装了多个Python版本:

然后在vscode上查看python版本:

python --version

你必须确保matplotlib是安装在当前的python版本.如果不是,让我们假设你有python 3.10在终端上,

python3.10 -m pip install matplotlib
gpnt7bae

gpnt7bae2#

我已经解决了这个问题,安装了微软c++分布式VS代码。不知道为什么或如何工作,看到它在YouTube视频,所以如果有人愿意解释这将是有帮助的。谢谢

相关问题