Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Python ';多收集';对象不可调用_Python_Python 2.7_Matplotlib_Tkinter - Fatal编程技术网

Python ';多收集';对象不可调用

Python ';多收集';对象不可调用,python,python-2.7,matplotlib,tkinter,Python,Python 2.7,Matplotlib,Tkinter,所以我得到的错误是TypeError:“PolyCollection”对象不可调用。我该如何解决这个问题 我试图用Python和Matplotlib创建一个动态更新的直方图,并使用Tkinter显示。正如您在下面的代码中所看到的,我使用Matplotlib的hexbin函数通过传递多个列表来创建图形;年龄、体重和身高。我创建的UpdateGraph方法是在一个单独的线程上生成的无限循环,它检查新数据是否已准备好绘制图表。如果是,则跳回到主线程,通过清除图形、绘制数据、添加轴标题,然后绘制画布来更

所以我得到的错误是
TypeError:“PolyCollection”对象不可调用
。我该如何解决这个问题

我试图用Python和Matplotlib创建一个动态更新的直方图,并使用Tkinter显示。正如您在下面的代码中所看到的,我使用Matplotlib的
hexbin
函数通过传递多个列表来创建图形;年龄、体重和身高。我创建的
UpdateGraph
方法是在一个单独的
线程
上生成的无限循环,它检查新数据是否已准备好绘制图表。如果是,则跳回到
主线程
,通过清除图形、绘制数据、添加轴标题,然后绘制画布来更新图形

创建图表(我很确定所有这些都没问题,不会引起任何问题。)

更新图形方法

def UpdateGraphs(self):
    while True:
        dataFinalIndex = len(self.mainController.PDWs)

        ages    = []
        weights = []
        heights = []

        for person in self.persons:
            ages.append(person.age)
            weights.append(person.weight)
            heights.append(person.height)

        root.after_idle(self.avhplot.clear)
        root.after_idle(self.wvhPlot.clear)

        """ Here is where the error comes from"""
        root.after_idle(self.avhplot.hexbin(heights, ages, cmap = matplotlib.cm.jet,
                                            mincnt = 1, bins = "log", extent = self.avhExtent))
        root.after_idle(self.avhplot.hexbin(heights, weights, cmap = matplotlib.cm.jet,
                                            mincnt = 1, bins = "log", extent = self.wvhExtent))
        """             End of Error          """

        root.after_idle(self.avhPlot.set_ylabel, ("Ages")
        #The rest of the labels are set like this as well

        root.after_idle(self.avhCanvas.draw)
        root.after_idle(self.wvhCanvas.draw)
当我试图绘制数据时抛出错误。我不知道为什么会发生这种情况,因为这在过去是行之有效的。奇怪的是,它会在第一次(即使有错误)绘制数据,但不会在第二次

堆栈跟踪

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 495, in callit
    func(*args)
TypeError: 'PolyCollection' object is not callable
    self.avhplot.hexbin(heights, ages, cmap = matplotlib.cm.jet,
                                        mincnt = 1, bins = "log", extent = self.avhExtent)
    self.avhplot.hexbin(heights, weights, cmap = matplotlib.cm.jet,
                                        mincnt = 1, bins = "log", extent = self.wvhExtent)

注意:上面的所有代码都只是示例。如果有任何打字错误,它们在我的实际代码中是不存在的。

我发现了问题所在-

我假设我需要跳回主线程以绘制数据,但出于某种原因,在清除和绘制数据的函数没有必要之前添加
root.add_idle
,从而导致问题

已更正的代码

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 495, in callit
    func(*args)
TypeError: 'PolyCollection' object is not callable
    self.avhplot.hexbin(heights, ages, cmap = matplotlib.cm.jet,
                                        mincnt = 1, bins = "log", extent = self.avhExtent)
    self.avhplot.hexbin(heights, weights, cmap = matplotlib.cm.jet,
                                        mincnt = 1, bins = "log", extent = self.wvhExtent)