python - Can't get a Histogram (matplotlib.pyplot.hist) to update for new data in tkinter -


i making gui using tkinter , matplotlib in python. displays data , graphs spread on several notebook tabs. user makes selections graphs , text update. working until added histogram. don’t know how change data or xlim , ylim.

the below code extract of code show how works.

import tkinter tk import tkinter.ttk ttk matplotlib.backends.backend_tkagg import figurecanvastkagg matplotlib.figure import figure import numpy np  root = tk.tk() root.geometry('1200x300')  def configframe(frame, num=50):   x in range(num):     frame.rowconfigure(x, weight=1)     frame.columnconfigure(x, weight=1)  def runprog():   mu, sigma = 0, .1   y = np.array(np.random.normal(mu, sigma, 100))   x = np.array(range(100))    lines2[1].set_xdata(x)   axs2[1].set_xlim(x.min(), x.max()) # need change limits manually    # know y isn't changing in example put in others see   lines2[1].set_ydata(y)   axs2[1].set_ylim(y.min(), y.max())    canvas2.draw()  configframe(root)  nb = ttk.notebook(root) # creates blamk line nb.grid(row=1, column=0, columnspan=2, rowspan=2, sticky='nesw')  mypage = ttk.frame(nb) configframe(mypage) nb.add(mypage, text="my page")  myframe = ttk.frame(mypage) myframe.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='nesw') configframe(myframe)  # there figure on tab fig2 = figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')  canvas2 = figurecanvastkagg(fig2, master=myframe) canvas2._tkcanvas.grid(row=2, column=0, columnspan=50, rowspan=47, sticky='nesw')  axs2 = [] lines2=[]  # there 4 other plots on page axs2.append(fig2.add_subplot(4,1,1))  mu, sigma = 0, .1 y = list(np.random.normal(mu, sigma, 100)) x = list(range(100)) # histogram of data n, bins, patches = axs2[0].hist(y, 25, normed=false) axs2[0].set_xlabel('x label') axs2[0].set_ylabel('y label') axs2[0].grid(true) lines2.append([]) # don't know how access histogram  axs2.append(fig2.add_subplot(4,1,2)) lines, = axs2[1].plot(x,y) lines2.append(lines)  fig2.canvas.draw()  runbutton = tk.button(mypage, text="change data", width=15, command=runprog) runbutton.grid(row=50, column=25, sticky='nw') root.update()  root.mainloop() 

ok, able achieve wanted clearing axis , recreating it. it's not terribly pythonic doesn't seem can change data .hist . other suggestions appreciated works.

the change made in runprog() method. included code below.

def runprog():   mu, sigma = 0, .1   y = np.array(np.random.normal(mu, sigma, 100))   x = np.array(range(100))    # it'a not python cleared axis , remadeit   axs2[0].cla()   n, bins, patches = axs2[0].hist(y, 25, normed=false)   axs2[0].set_xlabel('x label')   axs2[0].set_ylabel('y label')   axs2[0].grid(true)   #    axs2[1].set_xlim(x.min(), x.max()) # need change limits manually    # know y isn't changing in example put in others see   lines2[1].set_ydata(y)   axs2[1].set_ylim(y.min(), y.max())    canvas2.draw() 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -