[英]Errors with matplotlib and numpy
我正在尝试学习使用 matplotlib 并为此安装了 Anaconda3 和 VSCode。 使用 anaconda3,所有必需的库都已安装,但是当我尝试运行一个简单的代码时,出现了一堆错误。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fruits = \['apple', 'blueberry', 'cherry', 'orange'\]
counts = \[40, 100, 30, 55\]
bar_labels = \['red', 'blue', '\_red', 'orange'\]
bar_colors = \['tab:red', 'tab:blue', 'tab:red', 'tab:orange'\]
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
plt.show()
这是我在网上找到的一个简单代码,我尝试运行它以查看是否一切正常,但我得到了这个:
Traceback (most recent call last):
File "c:\Users\ahmed\Desktop\saved\mat1.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\ahmed\anaconda3\lib\site-packages\matplotlib\__init__.py", line 104, in <module>
import numpy
File "C:\Users\ahmed\anaconda3\lib\site-packages\numpy\__init__.py", line 150, in <module>
from . import core
File "C:\Users\ahmed\anaconda3\lib\site-packages\numpy\core\__init__.py", line 70, in <module>
from . import numerictypes as nt
File "C:\Users\ahmed\anaconda3\lib\site-packages\numpy\core\numerictypes.py", line 596, in <module>
_register_types()
File "C:\Users\ahmed\anaconda3\lib\site-packages\numpy\core\numerictypes.py", line 591, in _register_types
numbers.Integral.register(integer)
AttributeError: module 'numbers' has no attribute 'Integral'
错误是:错误我对这一切都是陌生的,所以我真的不知道该怎么做
在您的工作目录中有一个numbers.py
文件。 重命名或删除它,以便 Python(和 NumPy)将从标准库中获取正确的numbers
模块。
我认为您电脑上的库内部存在一些问题。
我自己检查了代码,在我这边工作正常。
另外,我认为代码中有一些不必要的退格键。 通过我的代码更新这行代码:
fruits = \['apple', 'blueberry', 'cherry', 'orange'\]
counts = \[40, 100, 30, 55\]
bar_labels = \['red', 'blue', '\_red', 'orange'\]
bar_colors = \['tab:red', 'tab:blue', 'tab:red', 'tab:orange'\]
具有实时结果的更新代码是:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', 'red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.