如何修复:模块“matplotlib”没有“plot”属性;
使用matplotlib时可能遇到的错误是:
AttributeError : module 'matplotlib' has no attribute 'plot'
当您使用以下代码导入 matplotlib 时,通常会出现此错误:
import matplotlib as plt
相反,您应该使用:
import matplotlib. pyplot as plt
以下示例展示了如何在实践中纠正此错误。
如何重现错误
假设我们尝试使用以下代码在 matplotlib 中创建线图:
import matplotlib as plt #define data x = [1, 2, 3, 4, 5, 6] y = [3, 7, 14, 19, 15, 11] #create line plot plt. plot (x, y) #show line plot plt. show () AttributeError : module 'matplotlib' has no attribute 'plot'
我们收到错误,因为我们使用了错误的代码行来导入 matplotlib 库。
如何修复错误
要修复此错误,只需使用正确的代码导入 matplotlib 库:
import matplotlib. pyplot as plt #define data x = [1, 2, 3, 4, 5, 6] y = [3, 7, 14, 19, 15, 11] #create line plot plt. plot (x, y) #show line plot plt. show ()
请注意,我们能够成功创建线图而不会收到错误,因为我们使用了正确的代码行来导入 matplotlib 库。
其他资源
以下教程解释了如何修复 Python 中的其他常见错误:
如何修复:没有名为 matplotlib 的模块
如何修复:没有名为 pandas 的模块
如何修复:没有名为 numpy 的模块