1

Good afternoon,

I am running Python 3.9.2 on my Mac (Mac OS Big Sur 11.2.3), and when running my code, that includes:

    import matplotlib.pylab as plt
             (...)
    cm = plt.get_cmap('viridis')
    cNorm = plt.colors.Normalize(vmin=min(forces), vmax=max(forces))
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)

I get the error:

    cNorm = plt.colors.Normalize(vmin=min(forces), vmax=max(forces))
    AttributeError: module 'matplotlib.pylab' has no attribute 'colors'

I have tried uninstalling and installing matplotlib (both with conda and pip), and that didn't work. I tried installing different versions of matplotlib, and that didn't work either. I looked at this thread but it didn't answer my questions.

I don't understand what the issue is here, since the code was provided by my instructor, and since the error is for the second use of plt, the error is not with the import of matplotlib.

Please let me know if you know what the issue is, or what I could try to fix this. Thank you.

  • 4
    The `colors` location in the API is a little different than what you're thinking it is. It's directly under `matplotlib`: `matplotlib.colors`. This is the convention for importing it: `import matplotlib.colors as mcolors`. – mechanical_meat Apr 28 '21 at 03:52
  • 1
    Also, you really shouldn't use `import matplotlib.pylab as plt`. Use`import matplotlib.pyplot as plt` instead. https://stackoverflow.com/questions/11469336/what-is-the-difference-between-pylab-and-pyplot – JohanC Apr 28 '21 at 11:26

0 Answers0