我一直在练习 matplotlib,然后短暂休息后,我再次打开代码,令我惊讶的是,出现了缺少成员的错误,这是我之前没有遇到过的。
Module 'matplotlib.cm' has no 'Blues' memberpylint(no-member)
事情是:
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.cm.Blues
<matplotlib.colors.LinearSegmentedColormap object at 0x000002D00AF42B20>
>>>
它就在那里。一点也不缺;
这是脚本:
import matplotlib.pyplot as plt
from random_walk_mod import RandomWalkMod
print("This program displays a random walk")
# Keep making new walks, as long s the program is active
while True:
# Make a random walk
rw = RandomWalkMod()
rw.fill_walk()
# Plot the points in the walk
plt.style.use('classic')
fig, ax = plt.subplots(figsize=(15,9), dpi=128)
point_numbers = range(rw.num_points)
ax.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,
edgecolors='none', s=1)
# Emphasize the first and last points
ax.scatter(0,0,c='green', edgecolors='none', s=100)
ax.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break
我在这个论坛找到了解决方案(https://www.zhihu.com/question/433655496),只需传入颜色而不是路径,如下:
cmap='Blues'
代替:
cmap=plt.cm.Blues
而且它有效。然而,它似乎并没有对我来说结束,因为我真的觉得必须有一个解决方法来编写路径(“plt.cm.Blues”,几周前工作正常),因为:
>>> dir(plt.cm) == dir(matplotlib.cm)
True
因此,调用“Blues”有两种可能的路径,但 VSCode 似乎没有实现它们。对象在那里,路径也写得正确,那么发生了什么? 甚至导入整个 matplotlib 也无法解决问题。 另外,我在 matplotlib 的文档中找不到任何相关内容。我希望我只是不擅长阅读文档:)
非常感谢任何帮助。谢谢!
[更新:已解决]
我对此有两种可能的解决方案,其中之一是:
所以我选择了后者,但是 VSCode 无法识别更新。无论我做什么(更新Python,更新VSCode,卸载并重新安装所有内容......),VSCode仍然不会显示更新后的python解释器(3.9.5),而是显示我的旧版本(3.9.1)。我正确设置了路径,但对于 VSCode 来说,就像我从未更新过 Python 一样。
因此,经过一番彻底的研究后,我尝试卸载 VSCode,但这次通过删除文件夹“Code”(Win+R,然后输入“%appdata”,按 Enter 键)来清除其缓存。最后,VSCode 按预期读取 python 应用程序,更好的是,linting 问题消失了。
你可以运行代码,对吧?
这是 pylint 的问题,而不是你的代码的问题。
可以参考官方文档,解释了pylint中的
no-member
问题。
您可以将其添加到
settings.json
文件中来修复此问题:
"python.linting.pylintArgs": [
"--c-extension-no-member",
]