1

I've wanted to try matplotlib for the first time but when I import it, there is an error. The only line of my code is:

from matplotlib import pyplot as plt

but when I run this, I have this error:

Traceback (most recent call last):
  File "/Users/mylaptop/Desktop/main.py", line 1, in <module>
    from matplotlib import pyplot as plt
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/matplotlib/__init__.py", line 903, in <module>
    rcParamsDefault = _rc_params_in_file(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/matplotlib/__init__.py", line 834, in _rc_params_in_file
    config[key] = val  # try to convert to proper type or raise
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/matplotlib/__init__.py", line 678, in __setitem__
    raise ValueError(f"Key {key}: {ve}") from None
ValueError: Key axes.prop_cycle: 'cycler('color', ['1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd', '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf'])' is not a valid cycler construction: name 'np' is not defined

I have no idea on how to fix it. I am on Mac and I use the Pycharm IDE.

Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32
Secu7or
  • 45
  • 7
  • Does https://stackoverflow.com/questions/33747933/bad-key-axes-prop-cycle-error-while-using-an-mplstyle-in-matplotlib-python help? – Karl Knechtel Oct 08 '21 at 20:02
  • I don't know if I am in the right version, how can I check it ? – Secu7or Oct 08 '21 at 20:04
  • when I try `sudo pip install matplotlib `, I got Requirement already satisfied multiple time – Secu7or Oct 08 '21 at 20:12
  • `python -m pip list` should show you everything that is installed in the current environment, with version numbers. Please don't use `sudo` for anything to do with `pip` unless you have confirmed that it is necessary. You may also need to make sure your version of Numpy is compatible. The installers are supposed to check version requirements, though. – Karl Knechtel Oct 08 '21 at 20:14
  • I got this : `matplotlib 2.2.5` – Secu7or Oct 08 '21 at 20:25

1 Answers1

0

Looking at the code of matplotlib/__init__.py, the Python library numpy is a requirement of matplotlib, but the error:

name 'np' is not defined

seems to tell us that it's not (correctly) installed in your system.

Davide Madrisan
  • 1,969
  • 2
  • 14
  • 22
  • but when I import numpy, it send the same error and it seems like I have bumpy installed (when I do the `python -m pip list` command): `numpy 1.8.0rc1` – Secu7or Oct 08 '21 at 22:02
  • When you import `numpy` you get a message telling that np is not defined? So this is definitely a numpy installation issue. Can you install a stable version instead? – Davide Madrisan Oct 08 '21 at 22:09
  • How can I do that ? – Secu7or Oct 09 '21 at 07:38
  • You can look [here](https://stackoverflow.com/questions/13916820/how-to-install-a-specific-version-of-a-package-with-pip). – Davide Madrisan Oct 09 '21 at 08:28
  • but I am already on the last version of numpy: `Requirement already satisfied: numpy in /usr/local/lib/python3.9/site-packages (1.21.2)` – Secu7or Oct 09 '21 at 13:43
  • Ok so it's not the version `1.8.0rc1` (see what you wrote yesterday). Also your initial log points to Python 3.8 and now you says `numpy` is installed for Python3.9. A package available in Python3.9 will not be seen by Python3.8 because the installation paths are different. Make sure matplotlib and numpy are installed both in the Python version you use when running your script. – Davide Madrisan Oct 09 '21 at 14:08