8

I have matplotlib lib installed using pip but when I run this code it gives me this error:

shar@shar-Lenovo-G50-30 ~ $ python3 opencv.py
Traceback (most recent call last):
  File "opencv.py", line 3, in <module>
    from matplotlib import pyplot as plt
ImportError: cannot import name 'pyplot'

My code is:

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

img1 = cv2.imread('/home/shar/home.jpg',0)  # queryImage
img2 = cv2.imread('/home/shar/home2.jpg',0) # trainImage

# Initiate SIFT detector
orb = cv2.ORB()

# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors.
matches = bf.match(des1,des2)

# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)

plt.imshow(img3),plt.show()

I also tried to install matplotlib from source and that still give me the error.

Steven Kryskalla
  • 14,179
  • 2
  • 40
  • 42
shar
  • 1,233
  • 6
  • 20
  • 30
  • 1
    What command did you use to install it, both with pip and from source? You're running the program with `python3`, is it possible you installed it for python 2 instead? Post the output of `pip --version` to see which python version it's using. – Steven Kryskalla Jul 25 '15 at 17:11
  • 2
    Have you tried changing your code to `import matplotlib.pyplot as plt`? I think you probably developed this code on Python 2 and are now trying to run it on Python 3 where [implicit relative imports won't work.](http://stackoverflow.com/questions/12172791/changes-in-import-statement-python3) – J Richard Snape Jul 25 '15 at 18:08
  • 1
    Why did you name your script `opencv.py`? Do you happen to have a `matplotlib.py` as well? What does `import matplotlib; print(matplotlib.__file__)` produce? – Martijn Pieters Jul 25 '15 at 19:09
  • pip 7.1.0 from /usr/local/lib/python3.4/dist-packages (python 3.4) – shar Jul 25 '15 at 19:13
  • i donnot have matplotlib.py – shar Jul 25 '15 at 19:14
  • @ J Richard Snape error – shar Jul 25 '15 at 19:16
  • Mmm - I thought about that comment straight after leaving it and thought that the conditions for that to be relevant were unlikely actually. You get *the same* error with `import matplotlib.pyplot as plt`? TBH @martijn's implication that there's another file called `matplotlib.py` somewhere that is 'masking' the module you intend seems highly likely. Can you post the output of his suggested diagnosis line i.e. `import matplotlib; pring(matplotlib.__file__)` then you run it in your `python3` interpreter? – J Richard Snape Jul 25 '15 at 20:56
  • i fixed it in python3 since few minutes thanks guys :) – shar Jul 25 '15 at 21:00
  • Great news! Can you let us know what the problem was (even better put a [self answer](http://stackoverflow.com/help/self-answer) so it's of benefit for future users) – J Richard Snape Jul 25 '15 at 21:03
  • ok i will do that :) – shar Jul 25 '15 at 21:04
  • 1
    ah sorry i forget that i used your line import matplotlib.pyplot as plt in python3 and it work but in python 2 not work please create a answer and i will take it :) – shar Jul 25 '15 at 21:08
  • 1
    hello from the future. It would have been really good to have an answer here, because it seems I have the exact same problem now. – Nemis Aug 11 '16 at 13:26

3 Answers3

14

Make sure you don't have any file names matplotlib.py that you have created if any then rename or delete that file. It worked for me

0

Did you get error saying "cannot import name 'pyplot' from partially initialized module 'matplotlib' " ?

You could have unknowingly saved your file name as matplotlib.py, since it comes as default file name suggestion. So change the file name and Bingo, you will not get that error again..

0

I had difficulty using pycharm to run the code below. But, once I switched to Visual Studio and made sure to install matplotlib and made sure my file name was not "matplotlib.py". I was able to run the code smoothly. Thank you for everyone's help.

from matplotlib import pyplot as plt

year = [1950, 1970, 1990, 2010] <br>
pop = [2.519, 3.692, 5.263, 6.972]

plt.plot(year, pop)<br>
plt.show()
DaveL17
  • 1,673
  • 7
  • 24
  • 38
  • For the record, PyCharm is capable of running the above code just fine. – DaveL17 Jul 09 '23 at 10:57
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34657403) – astoeriko Jul 11 '23 at 18:29