Last active
May 28, 2019 12:14
-
-
Save deeuu/5443e0e39c8a2439eccd14f7d87aeebf to your computer and use it in GitHub Desktop.
matplotlib + tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import tkinter as tk | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg | |
from matplotlib.figure import Figure | |
class Window(): | |
def __init__(self, root): | |
self.root = root | |
self.root.protocol("WM_DELETE_WINDOW", self.quit) | |
self.frame = tk.Frame(self.root) | |
self.frame.pack(expand=True) | |
button = tk.Button(master=self.frame, command=self.new_fig) | |
button.pack(side='bottom') | |
self.fig = None | |
self.new_fig() | |
def new_fig(self): | |
if self.fig: | |
self.fig.clear() | |
else: | |
self.fig = Figure() | |
canvas = FigureCanvasTkAgg(self.fig, master=self.frame) | |
self.widget = canvas.get_tk_widget() | |
self.widget.pack(side="top", fill='both', expand=True) | |
size = 10000 | |
data = [[random.gauss(1, 1) for _ in range(size)] for _ in range(size)] | |
ax = self.fig.add_subplot(111) | |
ax.imshow(data) | |
self.fig.canvas.draw() | |
def quit(self): | |
self.root.quit() | |
self.root.destroy() | |
if __name__ == '__main__': | |
root = tk.Tk() | |
w = Window(root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment