Commits

Matthew Turk committed 2cce0f1

Importing both the matplotlib example file and the first pass at a yt GUI,
using the pan-n-scan interface.

  • Participants

Comments (0)

Files changed (2)

simple_pan_scan.py

+from numpy import arange, sin, pi
+from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
+from matplotlib.figure import Figure
+import matplotlib.colors
+
+import Tkinter as Tk
+import tkFileDialog
+import sys
+from yt.mods import *
+import yt.extensions.image_panner as ip
+
+class SimpleWindow(object):
+    def __init__(self, master, pf = None):
+        frame = Tk.Frame(master)
+        frame.grid(row=0, column=0)
+
+        self.figure = Figure(figsize=(5,4), dpi=100)
+        self.canvas = FigureCanvasTkAgg(self.figure, master=master)
+        self.canvas.show()
+        self.canvas.get_tk_widget().grid(row=0, column=1)
+
+        self.zoom_10x_out_button = Tk.Button(frame, text="ZoomOut2x",
+                                             command=self.zoom_2x_out)
+        self.zoom_10x_out_button.grid(row=1, column=0)
+        self.zoom_10x_button = Tk.Button(frame, text="Zoom2x", command=self.zoom_2x)
+        self.zoom_10x_button.grid(row=1, column=1)
+
+        self.zoom_10x_out_button = Tk.Button(frame, text="ZoomOut10x",
+                                             command=self.zoom_10x_out)
+        self.zoom_10x_out_button.grid(row=2, column=0)
+        self.zoom_10x_button = Tk.Button(frame, text="Zoom10x", command=self.zoom_10x)
+        self.zoom_10x_button.grid(row=2, column=1)
+
+        self.textarea = Tk.Text(frame)
+        self.textarea.grid(row=3, columnspan=2)
+
+        self.setup_pf(pf)
+
+        self.axes = self.figure.add_subplot(111)
+        self.norm = matplotlib.colors.LogNorm()
+        self.image = self.axes.imshow(self.pan_n_scan.buffer, interpolation='nearest',
+                              origin='lower', norm = self.norm)
+        self.colorbar = self.figure.colorbar(self.image)
+        
+        self.button = Tk.Button(frame, text="QUIT", fg="red", command=frame.quit)
+        self.button.grid(row=0, columnspan=2)
+
+        self.canvas.draw()
+
+        # a tk.DrawingArea
+
+    def setup_pf(self, pf):
+        if pf is None:
+            pf = tkFileDialog.askopenfilename(filetypes = [('enzo files', '.hierarchy'), ('all files', '.*')])
+        if isinstance(pf, types.StringTypes):
+            if pf.endswith(".hierarchy"): pf = pf[:-10]
+            pf = load(pf)
+            if pf is None: raise RuntimeError
+        self.pf = pf
+        v, c = pf.h.find_max("Density")
+        self.center = c
+        sl = pf.h.slice(0, c[0], "Density", center=c)
+        self.pan_n_scan = pf.h.image_panner(sl, (512,512), "Density")
+        self.pan_n_scan.set_center(c)
+        self.pan_n_scan.zoom(2.0)
+
+    def zoom_2x(self):
+        self.pan_n_scan.zoom(2)
+        self._update_from_pns()
+
+    def zoom_2x_out(self):
+        self.pan_n_scan.zoom(0.5)
+        self._update_from_pns()
+
+    def zoom_10x(self):
+        self.pan_n_scan.zoom(10)
+        self._update_from_pns()
+
+    def zoom_10x_out(self):
+        self.pan_n_scan.zoom(.1)
+        self._update_from_pns()
+
+    def _update_from_pns(self):
+        buf = self.pan_n_scan.buffer
+        self.image.set_data(buf)
+        self.update_textarea(self.pan_n_scan.width, buf)
+        print "Autoscaling:", buf.min(), buf.max()
+        self.norm.autoscale((buf.min(), buf.max()))
+        self.image.set_norm(self.norm)
+        self.colorbar.update_bruteforce(self.image)
+        self.canvas.draw()
+
+    def update_textarea(self, width, buf):
+        self.textarea.delete(1.0, Tk.END)
+        self.textarea.insert(Tk.END, "Width set to %0.3e by %0.3e\n" % (width))
+        self.textarea.insert(Tk.END, "Limits:      %0.3e to %0.3e\n" % (buf.min(), buf.max()))
+        
+
+def destroy(e): sys.exit()
+
+pf = load("/Users/matthewturk/Research/data/DataDump0155.dir/DataDump0155")
+
+root = Tk.Tk()
+#root.wm_title("Embedding in TK")
+app = SimpleWindow(root)
+Tk.mainloop()
+

simple_plotting.py

+import matplotlib
+matplotlib.use('TkAgg')
+
+from numpy import arange, sin, pi
+from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
+from matplotlib.figure import Figure
+
+import Tkinter as Tk
+import sys
+
+def destroy(e): sys.exit()
+
+root = Tk.Tk()
+root.wm_title("Embedding in TK")
+#root.bind("<Destroy>", destroy)
+
+
+f = Figure(figsize=(5,4), dpi=100)
+a = f.add_subplot(111)
+t = arange(0.0,3.0,0.01)
+s = sin(2*pi*t)
+
+a.plot(t,s)
+
+
+# a tk.DrawingArea
+canvas = FigureCanvasTkAgg(f, master=root)
+canvas.show()
+canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
+
+toolbar = NavigationToolbar2TkAgg( canvas, root )
+toolbar.update()
+canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
+
+#button = Tk.Button(master=root, text='Quit', command=sys.exit)
+#button.pack(side=Tk.BOTTOM)
+
+Tk.mainloop()