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.

12
  • 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. Commented Jul 25, 2015 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. Commented Jul 25, 2015 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? Commented Jul 25, 2015 at 19:09
  • 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 :) Commented Jul 25, 2015 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. Commented Aug 11, 2016 at 13:26

3 Answers 3

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

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

Comments

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..

1 Comment

The question was actually answered in the comments. It was an import error due to Python 2/3 differences.
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()

2 Comments

For the record, PyCharm is capable of running the above code just fine.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

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.