title: How to embed a matplotllib histogram onto a tkinter GUI?
body: <p>I have this program that creates a histogram from a list of medians from randomly generated numbers within specific parameters (upper limit, sample size). How can I make it so that the histogram that is created appears in the tkinter GUI, right above the scale? I've looked everywhere and still can't figure it out. Thanks in advance! I am using python 3.</p>
  
  <p>Bonus question (no need to answer but if you would like to it would help a ton): How would I make it so that the scale controls the value of number_lists (which is basically the number of  times a median is generated) and then updates the histogram's appearance by adding the new calculated median?</p>
  
  <pre><code>import random
  import matplotlib, sys
  matplotlib.use('TkAgg')
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  from matplotlib.figure import Figure
  import matplotlib.pyplot as plt
  import matplotlib.mlab as mlab
  import numpy as np
  from tkinter import *
  from tkinter import ttk
  
  
  #root
  root = Tk()
  root.title("Sample Size and the Normal Distribution")
  
  #mainframe
  mainframe = ttk.Frame(root, padding = "3 3 12 12")
  mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
  mainframe.columnconfigure(0, weight=1)
  mainframe.rowconfigure(0, weight=1)
  
  def create_list(sampleSize, upperLimit):
      numbList = []
      sampleSize = int(sample.get())
      upperLimit = int(upper.get())
      while sampleSize &gt; 0:
          sampleSize -= 1
          randomNum = random.randrange(0,upperLimit+1)
          numbList.append(randomNum)
      numbList.sort(key=int)
      return numbList
  
  def medians_variance(median_list):
      sum_of_medians = sum(median_list)
      variance = sum_of_medians / len(median_list)
      return variance
  
  
  def median(numbList):
  
          srtd = sorted(numbList)
          mid = len(numbList)//2
          if len(numbList) % 2 == 0:
              return (srtd[mid-1] + srtd[mid]) / 2.0
          else:
              return srtd[mid]
  
  
  def main():
  
      number_lists = 10
      lists = []
      median_list = []
  
      binsize = 10
  
      for i in range(number_lists):
          lists.append(create_list(sampleSize, upperLimit))
  
      for current_list in lists:
          current_median = median(current_list)
          median_list.append(current_median)
          median_list.sort(key=float)
  
  
      plt.hist(median_list, binsize)
  
      plt.xlabel('Median Value', fontsize = 15)
      plt.ylabel('Frequency', fontsize = 15)
  
      plt.show()
  
  
      med = median(median_list)
      std = np.std(median_list)
      var = (std**2)
  
  
  #sampleSize Entry
  sample = StringVar()
  sampleSize = ttk.Entry(mainframe, width = 7, textvariable = sample)
  sampleSize.grid(column = 2, row = 1, sticky =(W, E))
  
  #upperLimit Entry
  upper = StringVar()
  upperLimit = ttk.Entry(mainframe, width = 7, textvariable = upper)
  upperLimit.grid(column = 2, row = 3, sticky = (W, E))
  
  #binsize Entry
  Bin = StringVar()
  binsize = ttk.Entry(mainframe, width = 7, textvariable = Bin)
  binsize.grid(column = 2, row = 5, sticky = (W, E))
  
  #sampleSize and upperLimit Labels
  ttk.Label(mainframe, text="Sample Size ").grid(column = 1, row = 1, sticky = W)
  ttk.Label(mainframe, text="Upper Limit ").grid(column = 1, row = 3, sticky = W)
  ttk.Label(mainframe, text="Bin Size").grid(column = 1, row = 5, sticky = W)
  
  #histogram embed
  f = Figure(figsize = (5,4), dpi=100)
  
  #button for new histogram
  button = ttk.Button(mainframe, text="New Histogram", command=main).grid(column=1, row=7, sticky=W)
  
  #scale
  scale = Scale(mainframe, from_=0, to=10, orient=HORIZONTAL,length=400).grid(column = 5, row = 12, sticky= S)
  
  for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
  
  sampleSize.focus()
  upperLimit.focus()
  root.bind('&lt;Return&gt;', main)
  
  root.mainloop()
  </code></pre>