0

I was just about to try out matplotlib for the first time, but I've run into a problem before I've even gotten started. I copied this code:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

plt.show()

as a sort of hello world to see how things work, but PyCharm is showing a red error in the import statements:

enter image description here

enter image description here

When I try to run the code I get

ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

I have no idea how this could be happening, and I didn't even write this script myself, so I thought maybe it was an issue with the package installation, but I uninstalled and reinstalled it and nothing changed.

It seems to resemble this and this, but both cases use Visual Studio not PyCharm, the answer to the first one seems to be specific to Visual Studio and the second one I can't understand enough to tell if it's a match.

I have never had this issue importing other packages.

3
  • 6
    Don't name your script matplotlib.py! Commented Aug 15, 2024 at 15:59
  • 3
    Just to add to the previous comment, see, e.g., stackoverflow.com/a/72848557/1862861. Commented Aug 15, 2024 at 16:02
  • 1
    @BigBen Thank you! I didn't even realise I'd done that. That solves it. I think I need to sleep. Commented Aug 15, 2024 at 16:05

1 Answer 1

0

Did you install Matplotlib? You can install it using this command-

pip install matplotlib
Sign up to request clarification or add additional context in comments.

1 Comment

Did you check the comments below the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.