2017-08-22 39 views
0

我不明白我應該使用哪種方法來清除我已經嵌入到我的tkinter程序中的matplotlib條形圖。如何清除tkinter中的嵌入式matplotlib條形圖?

我應該專注於擺脫畫布,還是應該專注於使用plt.cla或plt.clf()。我已經嘗試過,因爲我正在使用Pycharm,並且IDE不顯示任何其他方法供我使用。

我將在下面顯示我的代碼:

def show_graph(self): 

    self.infoLabel.config(text="") 

    c.execute("SELECT * FROM " + self.trackName.get() + " WHERE Year_Of_Event = ? AND Athlete_Year_Group = ? " 
                 "ORDER BY ATHLETE_TIME ASC", (self.eventYear.get(), 
                         self.yearGroup.get())) 
    self.athleteData = c.fetchall() 

    if self.athleteData is None: 
     self.infoLabel.configure(text="There are no records for the event in this year for this year group.") 
    else: 
     for self.item in self.athleteData: 
      self.athlete = self.item 
      self.x.append(self.athlete[1]) 
      self.truncatedTime = str(self.athlete[3]).replace((self.athlete[3])[5:], "") 
      self.y.append(float(self.truncatedTime)) 
      self.text += (self.athlete[1]+" ran the "+self.trackName.get()+" in "+self.athlete[3]+" and is from the" 
          " house, "+self.athlete[4]+"\n") 
      self.infoLabel.configure(text=self.text) 

    self.numOfValues = len(self.x) 
    self.ind = numpy.arange(self.numOfValues) 

    self.colorChoice = random.choice(["r", "y", "b", "g", "w", "k", "m", "c"]) 
    self.rect = self.ax.bar(self.ind, self.y, width=self.width, color=self.colorChoice) 

    # add some text for labels, title and axes ticks 
    self.ax.set_ylabel('Time') 
    self.ax.set_title('Times under which athletes ran a race.') 
    self.ax.set_xticks(self.ind) 
    self.ax.set_xticklabels(self.x) 

    def label(bars): 
     """ 
     Attach a text label in each bar displaying its height 
     """ 
     for self.rect in bars: 
      height = self.rect.get_height() 
      self.ax.text(self.rect.get_x() + self.rect.get_width()/2., 0.5 * height, int(height), 
         ha='center', va='bottom') 

    label(self.rect) 

    self.canvas.show() 
    self.canvas.get_tk_widget().pack(side=tkinter.BOTTOM, fill="x") 

    self.canvas._tkcanvas.pack(side=tkinter.BOTTOM, fill="x") 

def clear_all(self): 
    self.infoLabel.configure(text="") 

我想讓它使得clear_all功能進行從而清除條形圖這樣我就可以在圖表上繪製不同的數據的代碼。

我不知道我該如何去做這件事。很感謝任何形式的幫助。謝謝。

+0

您可能想要閱讀[mcve]。 – ImportanceOfBeingErnest

回答

0

要你要,因爲清除積(a.clear()/plt.clf()/plt.cla())之後,從self.xList1和self.yList1

self.xList1 = [] 
self.yList1 = [] 

刪除數據,你仍然有列表中的數據,並再次吸引他們清楚的圖形。

+0

感謝您的時間,但在嘗試之後,圖表仍會繪製以前數據的數據。我需要的是能夠清除情節。我沒有使用過圖,所以這就是爲什麼我很難使用(a.clear()/ plt.clf()/ plt.cla())。 – user8254375