所以我得到的錯誤是TypeError: 'PolyCollection' object is not callable
。 我該如何解決這個問題?'PolyCollection'對象不可調用
我想創建一個動態更新的直方圖與Python和Matplotlib,使用Tkinter顯示。正如你可以在下面的代碼中看到的,我使用Matplotlib的hexbin
函數通過傳入幾個列表來創建圖形;年齡,體重和身高。我創建的UpdateGraph
方法是在單獨的thread
上產生的無限循環,它檢查是否準備好繪製新數據。如果是,則跳回main thread
並通過清除它們,繪製數據,添加軸標題然後繪製畫布來更新圖形。該圖形的
創建(我敢肯定,這一切都是確定,不會導致任何問題。)
#avh = Age Vs Height
#wvh = Weight Vs Height
self.xLimit = (0,8)
self.avhYLimit = (0,99)
self.wvhYLimit = (0,300)
self.avhExtent = self.xLimit + self.avhYLimit
self.wvhExtent = self.xLimit + self.wvhYLimit
self.avhGraphFrame = Frame(frame)
self.avhGraphFrame.grid(row = 0, column = 0, sticky = N+S+E+W)
self.avhFigure = matplotlib.figure.Figure(figsize = (7,5), frameon = False)
self.avhCanvas = FigureCanvasTkAgg(self.avhfigure , master = self.avhGraphFrame)
self.avhPlot = self.avhFigure.add_subplot(111)
self.avhPlot.set_xlim(self.xLimit)
self.avhPlot.set_ylim(self.avhYLimit)
self.avhCanvas.get_tk_widget().pack(side = Tkinter.LEFT, fill = Tkinter.BOTH, expand = 1)
self.wvhGraphFrame = Frame(frame)
self.wvhGraphFrame.grid(row = 3, column = 1, columnspan = 2, sticky = N+S+E+W)
self.wvhFigure = matplotlib.figure.Figure(figsize = (7,5), frameon = False)
self.wvhCanvas = FigureCanvasTkAgg(self.wvhFigure , master = self.wvhGraphFrame)
self.wvhPlot = self.wvhFigure.add_subplot(111)
self.wvhPlot.set_xlim(self.xLimit)
self.wvhPlot.set_ylim(self.wvhExtent)
self.wvhCanvas.get_tk_widget().pack(side = Tkinter.LEFT, fill = Tkinter.BOTH, expand = 1)
更新圖法
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
注:所有的代碼上面只是樣品。如果有任何拼寫錯誤,它們在我的實際代碼中不存在。