python - My matplotlib bar-chart duplicates every time a function is run, how can I refresh my bar-chart? -


my bar chart created looking around internet here , bar chart seems stack same result on top of previous plot function run instead of changing values while taking data database.

the code below utilizes classes , consists of page created in tkinter alongside use of sqlite3 , function:

class viewtrackdata(tkinter.frame): def __init__(self, parent, controller):     tkinter.frame.__init__(self, parent)     # label1 = tkinter.label(self, text="this page track data shown.")     # label1.grid(row=50, column=70, sticky="nsew")      self.athletedata = ""     self.athlete = ""     self.position = 1     self.times = {}     self.roundfigup = ""     self.roundfigdown = ""     self.x = []     self.y = []     self.text = ""       self.trackname = tkinter.spinbox(self, values=tracktables, width=50, justify="center", font=entry_font,                                      foreground="#000000", background="#ffffff")     self.trackname.place(relx=0.15, rely=0.1)      self.eventyear = tkinter.entry(self, width=50, justify="center", font=entry_font, foreground="#000000",                                    background="#ffffff")     self.eventyear.insert(0, "the year event took place.")     self.eventyear.place(relx=0.15, rely=0.41)      self.yeargroup = tkinter.spinbox(self, from_=7, to=11, width=50, justify="center", font=entry_font,                                      foreground="#000000", background="#ffffff")     self.yeargroup.place(relx=0.15, rely=0.70)      self.filterbutton = tkinter.button(self, text="filter", bg="#383a39", fg="#ab97bd", width=15, height=3,                                        command=self.show_graph)     self.filterbutton.place(relx=0.24, rely=0.82)      self.infolabel = tkinter.label(self, height=25, width=40, wraplength=300, anchor="center", relief="groove", font=font_use)     self.infolabel.place(relx=0.52, rely=0.1)  def show_graph(self):     self.infolabel.configure(text="")     c.execute("select * " + self.trackname.get() + " year_of_event = ? , athlete_year_group = ? "                                                         "order athlete_time asc", (self.eventyear.get(), self.yeargroup.get()))     self.athletedata = c.fetchall()     if self.athletedata none:         self.infolabel.configure(text="there no records in year year group.")     else:         self.item in self.athletedata:             self.athlete = self.item             self.times.update({self.athlete[1] : self.position}) # adds data name of athlete key , position athlete placed value.             self.position += 1 # increments position value             self.x.append(self.times[self.athlete[1]]) # adds position in athlete placed argument x axis             self.truncatedtime = str(self.athlete[3]).replace((self.athlete[3])[5:],"") # turns value seconds athlete has run under string , slices off " seconds"             print(self.truncatedtime)             if int((self.truncatedtime)[3]) >= 5: # if statement rounds or down depending on first value after decimal point.                 self.roundfigup = math.ceil(float(self.truncatedtime))                 self.y.append(int(self.roundfigup))             else:                 self.roundfigdown = math.floor(float(self.truncatedtime))                 self.y.append(int(self.roundfigdown))             self.text += ("representing number " + str(self.times[self.athlete[1]]) + " " + str(self.athlete[1])                           + ", ")             self.infolabel.configure(text=self.text)             print(self.athlete)      plt.bar(self.x, self.y, label="athlete time bars", color="green")      plt.xlabel("athlete positions")     plt.ylabel("athlete time")     plt.title("bar chart, representing times of athletes.")     plt.legend()     plt.show() 

i have tried search answer can't seem able implement see program.

i apologize beforehand if question seems noob-ish you.

there 1 last thing request.

could explain me how should go using plt.xticks? in order change labels numbers actual names of athletes. denoted self.athlete[1].

every time show_graph called more data appended self.x , self.y:

self.x.append(self.times[self.athlete[1]])  ... if int((self.truncatedtime)[3]) >= 5:      self.roundfigup = math.ceil(float(self.truncatedtime))     self.y.append(int(self.roundfigup)) else:     self.roundfigdown = math.floor(float(self.truncatedtime))     self.y.append(int(self.roundfigdown)) 

the fix re-initialize self.x , self.y @ beginning of show_graph:

def show_graph(self):     self.x = []     self.y = []     ... 

the initialization of self.x , self.y happens within __init__ method happens once, when viewtrackdata frame instantiated.


ps. may interested in seeing this example shows how embed matplotlib figure inside tk app. 1 advantage of doing way showing graph not freeze rest of app's graphical user interface (e.g. menus , buttons).


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -