简体   繁体   English

pyplot不导入,抛出错误

[英]pyplot not importing, throwing error

  File "/home/darcey/python/opencv/matplotlib.py", line 3, in <module>
    from matplotlib import pyplot as plt
ImportError: cannot import name pyplot

That is the error I get when I run my script: 那是我运行脚本时遇到的错误:

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

img = cv2.imread('color.jpg', 0)
plt.imshow(img, cmap = 'grey', interpolation = 'bicubic')
plt.xTicks([]), plt.yTicks([])
plt.show

The issue here is that the file that you are running is called matplotlib.py as you can see from the first line of the traceback. 这里的问题是,您正在运行的文件名为matplotlib.py如您在回溯的第一行所见。

File "/home/darcey/python/opencv/matplotlib.py", line 3, in <module>
    from matplotlib import pyplot as plt
ImportError: cannot import name pyplot

This name "masks" the installed matplotlib module and therefore your code is trying to find pyplot within the current file. 此名称“掩盖”已安装的matplotlib模块,因此您的代码正在尝试在当前文件中查找pyplot

The moral of the story - never name your .py files the same as any of your installed modules. 这个故事的寓意-切勿将.py文件的名称与任何已安装的模块的名称相同。

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

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