Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matplotlib和Pyplot(Python/Tkinter)的导入问题_Python_Python 3.x_Matplotlib_Tkinter_Python Import - Fatal编程技术网

Matplotlib和Pyplot(Python/Tkinter)的导入问题

Matplotlib和Pyplot(Python/Tkinter)的导入问题,python,python-3.x,matplotlib,tkinter,python-import,Python,Python 3.x,Matplotlib,Tkinter,Python Import,我遇到了一个无法调用“matplotlib”的“pyplot”元素的问题。从下面的代码中可以看出,我不得不为mattplotlib元素添加一个“TkAgg”,这是一个常见问题 import matplotlib matplotlib.use("TkAgg") 但是,现在我无法将“.pyplot”添加到导入中。我尝试了以下方法: import matplotlib.pyplot as plt plt.use("TkAgg") 但这给了我一个错误: AttributeError:模块“matpl

我遇到了一个无法调用“matplotlib”的“pyplot”元素的问题。从下面的代码中可以看出,我不得不为mattplotlib元素添加一个“TkAgg”,这是一个常见问题

import matplotlib
matplotlib.use("TkAgg")
但是,现在我无法将“.pyplot”添加到导入中。我尝试了以下方法:

import matplotlib.pyplot as plt
plt.use("TkAgg")
但这给了我一个错误:

AttributeError:模块“matplotlib.pyplot”没有属性“use”

我的代码需要pyplot才能运行,但我无法在仍然必须使用“.use”(“TkAgg”)的情况下解决如何导入它的问题,因此我该如何解决这个问题


我正在运行Python 3.6.2,我正在使用Tkinter开发我的程序

这是两件完全不同的事情。导入
matplotlib
可以设置后端。然后仍然需要导入
pyplot
才能在以后使用它

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# ... rest of code
如果使用use()函数,则必须在导入matplotlib.pyplot之前执行此操作。在导入pyplot后调用use()将无效

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
检查:

matplotlib.get_backend()

好的,非常感谢@importanceofbeingernest解决了这个问题。但是我现在得到一个错误“TypeError:只有长度为1的数组可以转换为Python标量”当然,你可能会从一些你不在这里共享的代码中得到任何错误,并且没有办法帮助你处理未知代码。我知道,我不确定这是否与此相关。