简体   繁体   English

matplotlib:导入错误:无法导入名称“pyplot”

[英]matplotlib: ImportError: cannot import name 'pyplot'

I have matplotlib lib installed using pip but when I run this code it gives me this error:我使用 pip 安装了 matplotlib lib,但是当我运行此代码时,它给了我这个错误:

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.我还尝试从源代码安装 matplotlib,但仍然给我错误。

Make sure you don't have any file names matplotlib.py that you have created if any then rename or delete that file.确保您没有创建任何文件名 matplotlib.py(如果有),然后重命名或删除该文件。 It worked for me它对我有用

Did you get error saying "cannot import name 'pyplot' from partially initialized module 'matplotlib' " ?您是否收到错误消息“无法从部分初始化的模块‘matplotlib’导入名称‘pyplot’”?

You could have unknowingly saved your file name as matplotlib.py, since it comes as default file name suggestion.您可能在不知不觉中将文件名保存为 matplotlib.py,因为它是默认文件名建议。 So change the file name and Bingo, you will not get that error again..所以改变文件名和宾果,你不会再得到那个错误..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM