ostapdontstop

plot-emded

May 16th, 2020
1,122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter
  2.  
  3. from matplotlib.backends.backend_tkagg import (
  4.     FigureCanvasTkAgg, NavigationToolbar2Tk)
  5. # Implement the default Matplotlib key bindings.
  6. from matplotlib.backend_bases import key_press_handler
  7. from matplotlib.figure import Figure
  8.  
  9. import matplotlib.pyplot as plt
  10.  
  11. import numpy as np
  12. import random
  13.  
  14. def lcg(modulus, a, c, seed):
  15.     """Linear congruential generator"""
  16.     while True:
  17.         seed = (a * seed + c) % modulus
  18.         yield seed / modulus
  19.  
  20. lcg_gen = lcg(2**32, 1664525, 1013904223, 0)
  21.  
  22.  
  23. root = tkinter.Tk()
  24. root.wm_title("Embedding in Tk")
  25.  
  26. # fig = Figure(figsize=(5, 4), dpi=100)
  27. # t = np.arange(0, 3, .01)
  28. # fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
  29. fig, ax = plt.subplots(2,2,sharey=True)
  30. # plt.subplots_adjust(bottom=0.1)
  31. # plt.suptitle('ГПСЧ', size=18)
  32. fig.canvas.set_window_title('Графики ГПСЧ')
  33.  
  34.  
  35.  
  36. n = 1000
  37. ax[0,0].hist([random.random() for i in range(n)],20)
  38. ax[0,1].hist([next(lcg_gen) for i in range(n)],20)
  39.  
  40. ax[0,0].set_title('random()')
  41. ax[0,1].title.set_text('Линейно конгруэнтный')
  42.  
  43. canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
  44. # canvas.draw()
  45. canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
  46.  
  47. # toolbar = NavigationToolbar2Tk(canvas, root)
  48. # toolbar.update()
  49. # canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
  50.  
  51.  
  52. def on_key_press(event):
  53.     print("you pressed {}".format(event.key))
  54.     key_press_handler(event, canvas, toolbar)
  55.  
  56.  
  57. canvas.mpl_connect("key_press_event", on_key_press)
  58.  
  59. message = tkinter.StringVar()
  60.  
  61.  
  62. def _input(event = None):
  63.  
  64.     try:
  65.         n = int(message.get())
  66.     except ValueError:
  67.         return
  68.  
  69.     if n < 0:
  70.         return
  71.  
  72.     ax[0,0].clear()
  73.     ax[0,1].clear()
  74.     ax[0,0].set_title('random()')
  75.     ax[0,1].title.set_text('Линейно конгруэнтный')
  76.     ax[0,0].hist([random.random() for i in range(n)],20)
  77.     ax[0,1].hist([next(lcg_gen) for i in range(n)],20)
  78.     # plt.draw()
  79.     canvas.draw()
  80.  
  81.     # root.quit()     # stops mainloop
  82.     # root.destroy()  # this is necessary on Windows to prevent
  83.                     # Fatal Python Error: PyEval_RestoreThread: NULL tstate
  84.  
  85.  
  86. f_input = tkinter.Frame()
  87.  
  88. label = tkinter.Label(f_input,text='Длина')
  89. text = tkinter.Entry(f_input,textvariable=message)
  90. button = tkinter.Button(f_input,text="OK", command=_input)
  91.  
  92. button.config(width=4)
  93. text.insert(tkinter.END, str(n))
  94. text.bind('<Return>', _input)
  95.  
  96.  
  97. f_input.pack(side=tkinter.BOTTOM, pady=5)
  98.  
  99. label.pack(side=tkinter.LEFT, padx=10, pady=5)
  100. text.pack(side=tkinter.LEFT, padx=1, pady=5)
  101. button.pack(side=tkinter.LEFT, padx=10, pady=5)
  102.  
  103. text.focus_set()
  104. tkinter.mainloop()
  105. # If you put root.destroy() here, it will cause an error if the window is
  106. # closed with the window manager.
RAW Paste Data