2

I am making a gui using tkinter and matplotlib in python. It displays data and graphs spread over several notebook tabs. As the user makes certain selections the graphs and text update. Everything was working perfectly until I added a histogram. I don’t know how to change its data or xlim and ylim.

The below code is an extract of my code to show how it works.

import tkinter as tk
import tkinter.ttk as ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

root = tk.Tk()
root.geometry('1200x300')

def configFrame(frame, num=50):
  for 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()) # You need to change the limits manually

  # I know the y isn't changing in this example but I put it in for others to see
  lines2[1].set_ydata(y)
  axs2[1].set_ylim(y.min(), y.max())

  canvas2.draw()

configFrame(root)

nb = ttk.Notebook(root)
# This just creates a 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 is another figure on another 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 are 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))
# the histogram of the 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 to access it from 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()
Paul O
  • 425
  • 5
  • 19

1 Answers1

0

Ok, I was able to achieve what I wanted by clearing the axis and recreating it. It's not terribly pythonic but it doesn't seem that I can change the data for the .hist . Any other suggestions are appreciated but this works.

The only change that I made was in the runProg() method. I included the 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 really python but I just cleared the axis and 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()) # You need to change the limits manually

  # I know the y isn't changing in this example but I put it in for others to see
  lines2[1].set_ydata(y)
  axs2[1].set_ylim(y.min(), y.max())

  canvas2.draw()
Paul O
  • 425
  • 5
  • 19