简体   繁体   English

为什么 matplotlib 在 VSCode 上不起作用

[英]Why matplotlib is not working on the VSCode

I wrote a basic plotting code and I run it on the VS code my somehow the program cannot run it我写了一个基本的绘图代码,我在 VS 代码上运行它,我不知何故程序无法运行它

I deleted python itself and VS code and I downloaded them again, but the problem is not solved.我删除了 python 本身和 VS 代码,我再次下载它们,但问题没有解决。

import matplotlib.pyplot as plt
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)
plt.show()


Traceback (most recent call last):
File "c:/Users/xxx/Desktop/Cmpt Physics/dreams.py", line 1, in <module>
    import matplotlib.pyplot as plt
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py", line 138, in <module>
    from . import cbook, rcsetup
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\rcsetup.py", line 24, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\fontconfig_pattern.py", line 18, in <module>
    from pyparsing import (Literal, ZeroOrMore, Optional, Regex, StringEnd,
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\pyparsing.py", line 104, in <module>       
    import copy
File "c:\Users\xxx\Desktop\Cmpt Physics\copy.py", line 5, in <module>
    from pylab import plot,show,grid
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\pylab.py", line 1, in <module>
    from matplotlib.pylab import *
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pylab.py", line 218, in <module>
    from matplotlib.dates import (
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\dates.py", line 156, in <module>
    from matplotlib import rcParams
ImportError: cannot import name 'rcParams' from 'matplotlib' (C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py)

Any idea why this happens?知道为什么会这样吗?

It looks like you are running code from a directory that contains a module named copy which is shadowing the copy module from the stdlib ( c:\Users\xxx\Desktop\Cmpt Physics\copy.py looks to be the culprit).看起来您正在从包含名为copy的模块的目录中运行代码,该模块正在隐藏 stdlib 中的copy模块( c:\Users\xxx\Desktop\Cmpt Physics\copy.py看起来是罪魁祸首)。 Rename that file to something that isn't the same name as something in the stdlib and it should fix the issue.将该文件重命名为与 stdlib 中的名称不同的名称,它应该可以解决问题。

Typically shadowing the stdlib leads to odd results like this where it pulls in modules out of order or the wrong module and makes things break in an odd way.通常隐藏 stdlib 会导致像这样的奇怪结果,其中它会将模块无序或错误的模块拉入,并使事情以一种奇怪的方式中断。

I just had this issue on Windows.我刚刚在 Windows 上遇到了这个问题。 My issue was due to some Python packages installed using pip while other installed using conda .我的问题是由于使用pip安装的一些 Python 软件包而其他使用conda安装的。 I fixed it by uninstalling the pip packages and reinstalling everything with conda install.我通过卸载pip软件包并使用conda install 重新安装所有内容来修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM