2

I am trying to run this code.

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('games.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

But I keep getting this error

Traceback (most recent call last):
  File "mpl.py", line 3, in <module>
    from matplotlib import pyplot as plt
  File "/home/megha/matplotlib.py", line 3, in <module>
    from matplotlib import pyplot as plt
ImportError: cannot import name pyplot

I googled a solution for this but I am only getting that the matplotlib version needs to be upgraded. I tried that too, and its still showing the same error. I have also tried import matplotlib.pyplot as plt but I have python 2.7 version, so that didnt work either.

6
  • How did you install matplotlib? sudo apt-get install python-matplotlib? Commented Mar 3, 2017 at 11:44
  • @NickHale obviously not, if it comes from /home/megha/matplotlib.py Commented Mar 3, 2017 at 11:44
  • 3
    It's usually not a good idea to give your script the name of a module/package you are looking to import. Change the file name matplotlib.py to something else that you are not importing. Commented Mar 3, 2017 at 11:48
  • @NickHale I used sudo apt-get install python-matplotlib and also sudo pip install --upgrade matplotlib Commented Mar 3, 2017 at 11:49
  • @Abdou, whoa, didn't see that! Commented Mar 3, 2017 at 11:50

1 Answer 1

5

Use import matplotlib.pyplot as plt instead.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.