Module matplotlib.cm has no [color] member

Viewed 239

I've been practicing with matplotlib, then after a short break I opened the codes again and to my surprise, got this error of missing member, which I wasn't getting before.

Module 'matplotlib.cm' has no 'Blues' memberpylint(no-member)

The thing is:

  1. This supposedly missing member is is there, as it shows in the terminal:
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>
>>>

It is there. Not missing at all;

  1. plt stands for matplotlib.pyplot, not just matplotlib, as the error suggests.

Here is the script:

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

I have found a solution at this forum (https://www.zhihu.com/question/433655496), which is just pass in the color instead of the path, as in:

cmap='Blues'

Instead of:

cmap=plt.cm.Blues

And it works. However, it doesn't seem to wrap it up for me, because I really feel there has to be a workaround to writing the path ("plt.cm.Blues", which a few weeks ago was working fine), because:

  1. plt refers to matplotlib.pylint, NOT just matplotlib. Why is this error pointing at plt (i.e. matplotlib.pyplot) and reporting a missing member from matplotlib.cm (i.e. ignoring pyplot)?
  2. "cm" and its many colors are obviously there. A simple dir search proves it. Actually, both matplotlib and pyplot have an attribute called "cm", which are even the same object:
>>> dir(plt.cm) == dir(matplotlib.cm)
True

So, there are two possible paths for calling "Blues", but VSCode doesn't seem to reach them out. The objects are there and the paths are corectly written, so what is going on? Not even importing the whole matplotlib solves the problem. Also, I couldn't find anything related in matplotlib's docs. I hope I'm just very bad at reading docs :)

Any help is much appreciated. Thank you!

[UPDATE: solved] I had two possible solutions for this, which would be either:

  1. setting up pylint in settings.json so it would ignore the no-member error, or
  2. update Python, which I just had to do.

So I chose the latter, but then VSCode wouldn't recognize the update. No matter what I did (update Python, update VSCode, uninstall and reinstall everything...), VSCode would still not show the updated python interpreter(3.9.5), but instead the old version I had(3.9.1). I set the path correctly, but to VSCode it was like I'd never updated Python.
So after some thorough research, I tried uninstalling VSCode but this time clearing its cache by deleting the folder "Code" (Win+R, then type "%appdata", hit Enter). Finally, VSCode reads the python app as expected, and, even better, the linting problem is gone.

2 Answers

Is it possible that VSCode is using a different python interpreter than your system python that you're using in the terminal? My guess is the VSCode python has a different version of matplotlib that would account for the inconsistency across your two scripts. Your script worked just fine for me on python 3.9.5, with matplotlib 3.4.2

sample plot

You can run the code, right?

It's a problem with pylint instead of your code.

You can refer to the official docs, explaining the no-member problem in pylint.

And you can add this to the settings.josn file to fix this:

"python.linting.pylintArgs": [
    "--c-extension-no-member",
]
Related